-3

I want to multiply a * b:

a = ['a', 'e', 'y']    
b = [3, 2, 1]

and get:

c = ['a', 'a', 'a', 'e', 'e', 'y']
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Austin Bauer
  • 33
  • 1
  • 1
  • 4
    @olaf It is not a mathematics question. An operation such as `['foo']*3` is defined in Python. – Warren Weckesser Feb 17 '18 at 02:17
  • 1
    @Olaf A string times a number is a perfectly valid and defined operation in Python. Ex: `'a' * 3` is `'aaa'` in this case. – Spencer Wieczorek Feb 17 '18 at 02:18
  • @WarrenWeckesser: And it is even given in the tutorial. I know. But that does not make the operqation a [multiplication](https://en.wikipedia.org/wiki/Multiplication). **That** term is well defined. And for the other thing, OP should learn the language and show his code plus what the problem is. – too honest for this site Feb 17 '18 at 02:22
  • This has probably been asked before, please read the language's docs at https://docs.python.org and consider using a simple "for i in range" loop where you can c += [a[i]] * b[i] in each iteration. – Franco Roura Feb 17 '18 at 03:03
  • A better duplicate target: [Repeat each item in a list a number of times specified in another list](https://stackoverflow.com/q/33382474/7851470). – Georgy May 23 '20 at 09:58

7 Answers7

3

That can be done with sum(), zip() and a list comprehension like:

Code:

c = sum([[s] * n for s, n in zip(a, b)], [])

Test Code:

a = ['a', 'e', 'y']
b = [3, 2, 1]

c = sum([[s] * n for s, n in zip(a, b)], [])
print(c)

Result:

['a', 'a', 'a', 'e', 'e', 'y']
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
2

zip() is your friend here:

a = ['a', 'e', 'y']

b = [3, 2, 1]

c = []
for x, y in zip(a, b):
    c.extend([x] * y)

print(c)
# ['a', 'a', 'a', 'e', 'e', 'y']

Or with itertools.chain.from_iterable():

from itertools import chain

c = list(chain.from_iterable([x] * y for x, y in zip(a, b)))

print(c)
# ['a', 'a', 'a', 'e', 'e', 'y']
RoadRunner
  • 25,803
  • 6
  • 42
  • 75
1

You can try this:

a = ['a', 'e', 'y']    
b = [3, 2, 1]
new_list = [i for b in [[c]*d for c, d in zip(a, b)] for i in b]

Output:

['a', 'a', 'a', 'e', 'e', 'y']
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
1

The most basic loop approach would be this, in my opinion:

a = ['a', 'e', 'y']
b = [3, 2, 1]
c = []

for i in range(len(a)):
    c.extend(list(a[i]*b[i]))
FatihAkici
  • 4,679
  • 2
  • 31
  • 48
0

Fancy one liner:

>>> from itertools import chain
>>> a = ['a', 'e', 'y']
>>> b = [3, 2, 1]
>>> c = list(chain(*[x*y for x,y in zip(a,b)]))
>>> c
['a', 'a', 'a', 'e', 'e', 'y']
mshsayem
  • 17,557
  • 11
  • 61
  • 69
0

You don't actually need to import any modules, as I said in the comments, please consider reading the Python docs.

a = ['a', 'e', 'y']    
b = [3, 2, 1]
c = []

for i in range(len(a)):
    c += [a[i]] * b[i]

print(c)
Franco Roura
  • 807
  • 8
  • 26
0

Through collections.Counter you can do this:-

Try this:-

from collections import Counter
a = ['a', 'e', 'y']    
b = [3, 2, 1]
d = {x:y for x,y in zip(a,b)}
c = Counter(d)
print(list(c.elements())) #your output
Narendra
  • 1,511
  • 1
  • 10
  • 20