Repeating elements in list comprehension

Viewed 5926

I have this list comprehension:

[[x,x] for x in range(3)]

which results in this list:

[[0, 0], [1, 1], [2, 2]]

but what I want is this list:

[0, 0, 1, 1, 2, 2]

What's the easiest to way to generate this list?

7 Answers

My solution:

def explode_list(p,n):
    arr=[]
    track=0

    if n==0:
        return arr    
    while track<len(p): 
        m=1
        while m<=n:
            arr.append(p[track])
            m=m+1
        track=track+1

    return arr
Related