1

I have a list which consist of string and integers. I have to pop only integers and put that in a separate list. My code:

list1=['a','b','c','d','e','f','g','h',1,2,3]
list=[]
x=0
for i in list1:
     if isinstance(i,int) :
        list.append(i)
        list1.pop(x)
     x += 1

print(list1)
print(list)

Output of above code

['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 2]
[1, 3]

My question is: Why are all the integers not removed by my code? What is wrong with my code?

gung - Reinstate Monica
  • 11,583
  • 7
  • 60
  • 79
geeksam
  • 33
  • 8

1 Answers1

7

You iterate and manipulate the same list at the same time:

for i in list1:            # iteration
    if isinstance(i,int):
        list.append(i)
        list1.pop(x)       # manipulate
     x += 1

This usually does not work: the for loop works with a cursor. If you meanwhile removes an item, you thus will start skipping elements.

It is better to simply use a declarative and Pythonic approach, like for instance the following:

list_int = [i for i in list1 if isinstance(i,int)]
list1 = [i for i in list1 if not isinstance(i,int)]

Furthermore you should not name a variable list, since then you remove the reference to the list class.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • it works!! but is it possible to modify "list1" Willem ji? – geeksam Jun 30 '17 at 15:16
  • @geeksam: not without the iteration getting into trouble. Note that popping from a list is furthermore an *O(n)* operation. So even if that was possible, it would result in an *O(n^2)* algorithm. – Willem Van Onsem Jun 30 '17 at 15:18