how to insert the elements in a list properly?

Dan Sommers dan at tombstonezero.net
Wed Apr 9 09:42:59 EDT 2014


On Wed, 09 Apr 2014 21:09:37 +0800, length power wrote:

> words = ["x1", "x2", "x3", "x4", "x5"]
> words.append(words.pop(2))
> words.append(words.pop(2))
> words
> ['x1', 'x2', 'x5', 'x3', 'x4']
> why i can't write it as:
> 
> [words.append(words.pop(2)) for i in range(0,2)]
> 
>>>> [words.append(words.pop(2)) for i in range(0,2)]
> [None, None]

You can, but you don't want to.  At this point, even though the
comprehension returned something else, words contains what you want.

HTH,
Dan



More information about the Python-list mailing list