make elements of a list twice or more.

L

liuerfire Wang

Sorry for the title which didn't make clear.

Here is a list x = [b, a, c] (a, b, c are elements of x. Each of them are
different type). Now I wanna generate a new list as [b, b, a, a, c, c].

I know we can do like that:

tmp = []
for i in x:
tmp.append(i)
tmp.append(i)

However, I wander is there a more beautiful way to do it, like [i for i in
x]?

Thanks.


--
Best regards.
/**********************************
google+: +liuerfire <http://gplus.to/onepiece> twitter:
@liuerfire<https://twitter.com/#!/liuerfire>
µ°ÌÛ²»µ°Ì۵Ķ¼¿ÉÒÔÊÔ×ŵãÒ»ÏÂ~^_^~ <http://db.tt/YGEdRM0>
***********************************/
 
A

alex23

Here is a list x = [b, a, c] (a, b, c are elements of x. Each of them
are different type). Now I wanna generate a new list as [b, b, a, a, c, c].

from itertools import chain

new_list = list(chain.from_iterable(zip(x,x)))
 
T

Tobiah

Sorry for the title which didn't make clear.

Here is a list x = [b, a, c] (a, b, c are elements of x. Each of them are different type). Now I wanna generate a new list as [b,
b, a, a, c, c].

If you don't care about the order, you can do:

x = x + x
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top