pythonstringlistnon-repetitive

How to generate a list of to repetitive items with different frequency?


I want to generate a list of strings shown below:

['EEG', 'EEG', 'EEG', 'EMG', 'EMG', 'EOG']

but, for example, with 32 'EEG' items and 2 'EMG' and one 'EOG'. How could I do that in one line?

I have read a StackOverflow post and I know a single repeated element can be generated with the code below:

['EEG']*32

or

['EEG' for _ in range(32)]

But I want a comprehensive list of all items with different frequencies. I want a flat list of different items, not a nested list of lists.


Solution

  • You can repeat a list element using multiplication operator and then just add the lists together

    answer = ['EEG']*32 + ['EMG']*2 + ['EOG']  
    # ['EEG', 'EEG', 'EEG', 'EEG', 'EEG', 'EEG', 'EEG', 'EEG', 'EEG', 'EEG', 'EEG', 'EEG', 'EEG', 'EEG', 'EEG', 'EEG', 'EEG', 'EEG', 'EEG', 'EEG', 'EEG', 'EEG', 'EEG', 'EEG', 'EEG', 'EEG', 'EEG', 'EEG', 'EEG', 'EEG', 'EEG', 'EEG', 'EMG', 'EMG', 'EOG']