Saturday, 15 January 2011

Duplicate element in python list -



Duplicate element in python list -

i have list in python:

l = ['a', 'c', 'e', 'b']

i want duplicate each element next original.

ll = ['a', 'a', 'c', 'c', 'e', 'e', 'b', 'b']

the order of elements should preserved.

>>> l = ['a', 'c', 'e', 'b'] >>> [x pair in zip(l,l) x in pair] ['a', 'a', 'c', 'c', 'e', 'e', 'b', 'b']

or

>>> itertools import repeat >>> [x item in l x in repeat(item, 2)] ['a', 'a', 'c', 'c', 'e', 'e', 'b', 'b']

python list list-comprehension

No comments:

Post a Comment