Creating a list in Python with multiple copies of a given object in a single line.

Posted by Deniz on Stack Overflow See other posts from Stack Overflow or by Deniz
Published on 2010-05-07T03:10:41Z Indexed on 2010/05/07 3:18 UTC
Read the original article Hit count: 328

Filed under:
|

Suppose I have a given Object (a string "a", a number - let's say 0, or a list ['x','y'] )

I'd like to create list containing many copies of this object, but without using a for loop:

L = ["a", "a", ... , "a", "a"]

or

L = [0, 0, ... , 0, 0]

or

L = [['x','y'],['x','y'], ... ,['x','y'],['x','y']]

I'm especially interested in the third case. Thanks!

© Stack Overflow or respective owner

Related posts about python

Related posts about list