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
  hi need help to make this code work correctly atulkul1985 5 702 Nov-20-2023, 04:38 PM
Last Post: deanhystad
  newbie question - can't make code work tronic72 2 626 Oct-22-2023, 09:08 PM
Last Post: tronic72
  Why doesn't calling a parent constructor work with arbitrary keyword arguments? PurposefulCoder 4 870 Jun-24-2023, 02:14 PM
Last Post: deanhystad
  Code works but doesn't give the right results colin_dent 2 674 Jun-22-2023, 06:04 PM
Last Post: jefsummers
  Beginner: Code not work when longer list raiviscoding 2 765 May-19-2023, 11:19 AM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,681 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Code used to work 100%, now sometimes works! muzicman0 5 1,384 Jan-13-2023, 05:09 PM
Last Post: muzicman0
  color code doesn't work harryvl 1 842 Dec-29-2022, 08:59 PM
Last Post: deanhystad
  Something the code dont work AlexPython 13 2,129 Oct-17-2022, 08:34 PM
Last Post: AlexPython
  cannot get code to work Led_Zeppelin 10 2,315 Jun-30-2022, 06:28 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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