1

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.

lenz
  • 5,658
  • 5
  • 24
  • 44
  • Possible duplicate of [Creating a list in Python with multiple copies of a given object in a single line](https://stackoverflow.com/questions/2785954/creating-a-list-in-python-with-multiple-copies-of-a-given-object-in-a-single-lin) – Remolten Apr 29 '19 at 20:50
  • @RemingtonThurber it is a little different question, and I have got a simple and good answer here! – Mohammad Javad Apr 29 '19 at 20:55
  • If you already know how to replicate an element, where are you stuck? Concatenating or joining lists is a straight look-up. – Prune Apr 29 '19 at 23:46
  • @prune yes, you are right, I did it by using the answer from Sheldore. – Mohammad Javad Apr 29 '19 at 23:57

5 Answers5

3

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']
Sheldore
  • 37,862
  • 7
  • 57
  • 71
1

Use the multiplication operator then concatenate, like so:

['EEG']*32 + ['EMG']*2 + ['EOG']
kudeh
  • 883
  • 1
  • 5
  • 16
1

You can use the collections.Counter.elements method:

from collections import Counter
list(Counter(EEG=32, EMG=2, EOG=1).elements())

This returns:

['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']
blhsing
  • 91,368
  • 6
  • 71
  • 106
0

dynamic way to do this would be:

config = [{"str":"EEG","freq":32},{"str":"EMG","freq":2},{"str":"EOG","freq":1}]
[item for sublist in ([[x["str"]]*x["freq"] for x in config]) for item in sublist]

to break this down:

config is an array of objects where str key is the string and freq key is the number of times you'd like to repeat it

([[x["str"]]*x["freq"] for x in config]) returns an array of arrays of repeated strings

the outer part flattens this complex array into a simple list

AlexYes
  • 4,088
  • 2
  • 15
  • 23
  • 1
    @Mohammad Javad Generally, it's called "list comprehension" and it is a handy technique when it comes to manipulating the lists. You can check this: https://www.pythonforbeginners.com/basics/list-comprehensions-in-python. In this particular case, I have used nested comprehension (one comprehension inside another one). Check the output of `([[x["str"]]*x["freq"] for x in config])` and you'll see what it does, and the outer statement gets you the final result from that. – AlexYes May 03 '19 at 11:31
0

You can either use the multiplication operator and then concatenate each list like this:

list = ['EEG'] * 32 + ['EMG'] * 2 + ['EOG']

Or you could also import Counter from collections, like another user stated, like this:

from collections import Counter
list(Counter(EEG=32, EMG=2, EOG=1).elements())

Both of these would yield a list containing 32 'EEG', 2 'EMG' and 1 'EOG'

seb_rc
  • 43
  • 3