0
Why doesn't this code work?
I'm trying to remove items from the list A and add them to the list B using for loop, here is my code, Idk why it doesn't work. a = ['John', 'Jack', 'James'] b = [] For i in a: c = a.pop(i) b.append(c) print(b) I know how to do this using while loop, but I want to do it using just one for loop, is it possible?
7 Answers
+ 4
Parameter (pop):
>> string.pop(index)
To get index of an element from the list:
>> list.index(string)
The rest is up to you.
+ 2
because pop() takes an index not a string.
in each iteration, you feed pop() a name instead of the name's index
+ 2
Steve Sajeev you were right tho...
And even when you pop indexes in a loop you'll get the index out of range error because you are shortening the list.
to do it right you'll need to just call pop() or pop(0), i'll add code
King
https://code.sololearn.com/c2nHw722JRhD/?ref=app
+ 2
Your "f" in For should be lowercase and your pop must be followed by an index not a string
+ 2
Here are some solutions đ
https://code.sololearn.com/cAxXb680H2bT/?ref=app
+ 1
Oo sorry for the wrong information I gaveđąđȘđČđąđȘ
0
Hi