# Repeat the first element of a list of lists according to the length

I have a Python list of lists:

``````l = [[1, 2, 3], , [5, 6], [7, 8, 9, 10]]
```
```

What I want is to repeat the first element of each list based on the length of the list:

``````result = [1, 1, 1, 4, 5, 5, 7, 7, 7, 7]
```
```

I can achieve this using list comprehension but my list is very long and so the method is slow:

``````result = [[x]*len(x) for x in l]

[[1, 1, 1], , [5, 5], [7, 7, 7, 7]]
```
```

Although, this still returns a list of lists rather than a flat list. So, I am trying to figure out the fastest method for creating that flat list based on the criteria mentioned above.

Update: I want the fastest performing method since the list is long

Using `itertools.repeat` with `chain` is the most efficient using python 2:

``````In : l = [choice(l) for _ in xrange(1000000)]
In : timeit list(itertools.chain(*[[i]*len(i) for i in l]))
1 loops, best of 3: 416 ms per loop

In : timeit [i for i in l for _ in xrange(len(i))]
1 loops, best of 3: 245 ms per loop

In : timeit list(itertools.chain.from_iterable(repeat(i,len(i)) for i in l))
1 loops, best of 3: 223 ms per loop

In : timeit [i for x in l for i in [x]*len(x)]
1 loops, best of 3: 332 ms per loop
```
```

Interestingly using `python3`, using a list instead of a generator expression is faster:

``````In : timeit list(chain.from_iterable(repeat(i, len(i)) for i in l))
1 loops, best of 3: 372 ms per loop

In : timeit [i for i in l for _ in range(len(i))]
1 loops, best of 3: 433 ms per loop

In : timeit list(chain.from_iterable([repeat(i,len(i)) for i in l]))
1 loops, best of 3: 296 ms per loop

In : timeit list(chain(*[[i]*len(i) for i in l]))
1 loops, best of 3: 460 ms per loop

In : timeit [i for x in l for i in [x]*len(x)]
1 loops, best of 3: 348 ms per loop
```
```

If you want a compromise between time and space then iterate over the chain object getting an element at a time:

``````In : %%timeit
for ele in chain.from_iterable([repeat(i,len(i)) for i in l]):
pass
....:
1 loops, best of 3: 306 ms per lo
```
```