This is my first post.I found interesting thing while playing with lists.

l = ['asd',"asd'das",'qw','213']
print "original list:", l
print "first item:", l.pop(0)
print "second item:", l.pop(1)
print "modified list:", l

original list:
first item: asd
second item: qw
modified list: ["asd'das", '213']

Why is it deleting third item,why not second one?
What I think is beacuse of single quote enclosed within double quotes and pop() function failed to handle this.But pop() doesn't show any error, instead it deletes third item..!

remove() function removes second item properly.

What may be the reason for this kind of behavoiur. Any idea?
BYW, I am using python2.7.

6 Years
Discussion Span
Last Post by razax
Featured Replies
  • After the first [icode]l.pop(0)[/icode], the list is [icode]["asd'das",'qw','213'][/icode], so the second item is 'qw'. Read More

list = [1, 2, 3, 4]

index[0] = 1
index[1] = 2
index[2] = 3
index[3] = 4

l.pop(0) --> 1
l.pop() --> 4

new list ==> [2, 3]

index 0 --> 2
index 1 --> 3

l.pop(1) --> 3

Edited by -ordi-: n/a

>>> l = ['asd',"asd'das",'qw','213']
>>> l
['asd', "asd'das", 'qw', '213']
>>> l.pop(0)
>>> l
["asd'das", 'qw', '213']
>>> l.pop(1)
>>> l
["asd'das", '213']

This is what`s happens.
l.pop(0) take out take out 'asd'
Then "asd'das" take place as element 0
Next when you use l.pop(1) it will take our element 1 'qw'
Maybe you get confused that "asd'das" is one element in the list.

Edited by snippsat: n/a


When lis pop. it removes the item from the stack.

Take it like you got items ...

llist = [1,2,3,4,5,6,7,]
removes  : 4th item

The pop means give out. So when you give out, you dont still have that.
Like you cant eat your cake and have it stuff. :)
Hope you get the idea.....

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.