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?

12th Jan 2021, 12:44 PM
King
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.
12th Jan 2021, 12:55 PM
noteve
noteve - avatar
+ 2
because pop() takes an index not a string. in each iteration, you feed pop() a name instead of the name's index
12th Jan 2021, 12:49 PM
Slick
Slick - avatar
+ 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
12th Jan 2021, 12:54 PM
Slick
Slick - avatar
+ 2
Your "f" in For should be lowercase and your pop must be followed by an index not a string
12th Jan 2021, 2:48 PM
Steven Wen
Steven Wen - avatar
+ 2
Here are some solutions 😄 https://code.sololearn.com/cAxXb680H2bT/?ref=app
12th Jan 2021, 3:12 PM
Stefanoo
Stefanoo - avatar
+ 1
Oo sorry for the wrong information I gave😱đŸ˜ȘđŸ˜Č😱đŸ˜Ș
12th Jan 2021, 12:51 PM
Steve Sajeev
Steve Sajeev - avatar
0
Hi
13th Jan 2021, 2:10 PM
Positive Energy
Positive Energy - avatar