0

With below code, I was expecting to swap two lists and was expecting all the elements from 'names' list to 'Excluded_list'using Pop() function. But it is not working expectedly, can someone please tell the issue with below code ??

names=['Ram','Sham','Mohan','John','Kuldeep']
Excluded_list=[]
print('names list',names)
print('Excluded list ',Excluded_list)

Code to swap/reprint lists

for name in names:
    Excluded_list.append(names.pop())
print('names list',names)
print('Excluded list ',Excluded_list)
user1729210
  • 579
  • 1
  • 8
  • 30

1 Answers1

1

You are modifying "names" in the for-loop, which means that the "for name in names" does not evaluate as you expect.

Replace the for-loop with a while-loop.

while len(names):
    Excluded_list.append(names.pop())
Kim Nyholm
  • 1,055
  • 1
  • 10
  • 19