Python Forum

Full Version: Why doesn't this code work?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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))
the for-loop iterates the elements of the list l.

What does the body of the for-loop do to l?
(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.
(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
(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]