Hi,

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

output:
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..!

l.remove("asd'das")
remove() function removes second item properly.

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

Recommended Answers

All 5 Replies

After the first l.pop(0) , the list is ["asd'das",'qw','213'] , so the second item is 'qw'.

commented: nice point +13
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
>>> l = ['asd',"asd'das",'qw','213']
>>> l
['asd', "asd'das", 'qw', '213']
>>> l.pop(0)
'asd'
>>> l
["asd'das", 'qw', '213']
>>> l.pop(1)
'qw'
>>> 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.

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

Take it like you got items ...

llist = [1,2,3,4,5,6,7,]
llist.pop(3)
##
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.....

Thanks for all your reponse..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.