Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why doesn't this code work?
#1
Hello everyone,
The following code didn't work while exercising on the lists. Could you tell me why the code doesn't work?

l=list(range(10))
for x in l:
    l+=str(x)
Or,
l=list(range(10))
for x in l:
    l.append(str(x))
Reply
#2
the for-loop iterates the elements of the list l.

What does the body of the for-loop do to l?
Reply
#3
(Jan-12-2018, 01:12 PM)mpd Wrote: the for-loop iterates the elements of the list l.

What does the body of the for-loop do to l?

Oh, okey Smile . Thank you so much Shy

All I want is to convert the elements on the list to string.
Reply
#4
(Jan-12-2018, 01:19 PM)hsezgin Wrote:
(Jan-12-2018, 01:12 PM)mpd Wrote: the for-loop iterates the elements of the list l.

What does the body of the for-loop do to l?

Oh, okey Smile . Thank you so much Shy

All I want is to convert the elements on the list to string.

Check out list comprehensions: https://docs.python.org/3/tutorial/datas...rehensions
Reply
#5
(Jan-12-2018, 01:30 PM)mpd Wrote:
(Jan-12-2018, 01:19 PM)hsezgin Wrote: Oh, okey Smile . Thank you so much Shy

All I want is to convert the elements on the list to string.

Check out list comprehensions: https://docs.python.org/3/tutorial/datas...rehensions

Okey, thank you so much Shy
I found the solution.
l=[str(i) for i in l]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PIP doesn't work YKR 1 595 Mar-28-2025, 02:10 PM
Last Post: snippsat
  I'm trying to install python 3.11.11 on windows 10 - it doesn't work Petonique 2 1,617 Feb-04-2025, 05:42 PM
Last Post: snippsat
  Can't get graph code to work properly. KDDDC2DS 1 624 Sep-16-2024, 09:17 PM
Last Post: deanhystad
  I can't for the life of me get this basic If statement code to work CandleType1a 8 2,113 May-21-2024, 03:58 PM
Last Post: CandleType1a
  Extending list doesn't work as expected mmhmjanssen 2 1,332 May-09-2024, 05:39 PM
Last Post: Pedroski55
  hi need help to make this code work correctly atulkul1985 5 1,872 Nov-20-2023, 04:38 PM
Last Post: deanhystad
  newbie question - can't make code work tronic72 2 1,487 Oct-22-2023, 09:08 PM
Last Post: tronic72
  Why doesn't calling a parent constructor work with arbitrary keyword arguments? PurposefulCoder 4 1,876 Jun-24-2023, 02:14 PM
Last Post: deanhystad
  Code works but doesn't give the right results colin_dent 2 1,385 Jun-22-2023, 06:04 PM
Last Post: jefsummers
  Beginner: Code not work when longer list raiviscoding 2 1,625 May-19-2023, 11:19 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020