How can I create a list like this:
["a","a","a",... (repeating "a" a hundred times") "b","b","b",(repeating "b" a hundred times.. )]
If I write ["a","b"] * 100
, then I get ["a","b","a","b",...]
, which is not exactly what I want.
Is there any function as simple as this one but gives me the result I want?
Just sum two lists produced by multiplication first and second elements:
['a']*100 + ['b']*100
It's faster than list comprehension and sort:
python -m timeit "sorted(['a', 'b']*100)"
100000 loops, best of 3: 9.76 usec per looppython -m timeit "[x for x in ['a', 'b'] for y in range(100)]"
100000 loops, best of 3: 5.15 usec per looppython -m timeit "['a']*100 + ['b']*100"
1000000 loops, best of 3: 1.86 usec per loop