Why is output 123123 instead of 123 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Why is output 123123 instead of 123

mylist = [] mylist.append(1) mylist.append(2) mylist.append(3) print(mylist[0]) # prints 1 print(mylist[1]) # prints 2 print(mylist[2]) # prints 3 # prints out 1,2,3 for x in mylist:      print x

30th May 2018, 11:39 AM
Chris Sheffield
Chris Sheffield - avatar
3 Answers
+ 5
Of course it will give you 1 2 3 1 2 3 You are first printing all the elements of the list serperatley. Then you are using another for loop to print them again. It's like repeating the same process. So first "123" for the individual print statements and another "123" for the for loop one. You can see this... https://code.sololearn.com/ch7YvObuOqLu/?ref=app
30th May 2018, 12:57 PM
Aaron Stone
Aaron Stone - avatar
+ 3
says so in the comments, it prints out each element in the list one by one before printing out the entire list
30th May 2018, 11:56 AM
hinanawi
hinanawi - avatar
+ 1
How does x represent all numbers in list?
30th May 2018, 10:04 PM
Chris Sheffield
Chris Sheffield - avatar