By C8H10N4O2


2018-03-13 15:12:24 8 Comments

I have a nested list of something that I would like to transform by repeating each element a given number of times specified in another nested list (of identical structure).

Example:

phrases = [ ['Happy birthday to you','Happy birthday dear Einstein'],
            ['Happy birthday to you']
          ]

reps = [ [2, 1],
         [1]
       ]

#------- Desired output looks like:           -------------------------------------------
out = [ ['Happy birthday to you','Happy birthday to you','Happy birthday dear Einstein'],
        ['Happy birthday to you']
      ] 

Using a nested loop via a list comprehension, I have tried:

[ [phrases[i][j] for rep in range(reps[i][j])] 
   for i in range(len(phrases)) 
     for j in range(len(phrases[i])) ]
#Returns:
#[['Happy birthday to you', 'Happy birthday to you'],
# ['Happy birthday dear Einstein'],
# ['Happy birthday to you']]

Which is not quite what I want.

2 comments

@jpp 2018-03-13 15:15:09

You can use numpy.repeat combined with zip for this:

import numpy as np

res = [np.repeat(i, j).tolist() for i, j in zip(phrases, reps)]

# [['Happy birthday to you',
#   'Happy birthday to you',
#   'Happy birthday dear Einstein'],
#  ['Happy birthday to you']]

@C8H10N4O2 2018-03-13 15:32:12

For me, the simplicity of this approach compared to the pure base list comprehension outweighs the additional dependency. Thanks to you and @Ajax1234 for your answers.

@Ajax1234 2018-03-13 15:19:14

You can use zip:

phrases = [ ['Happy birthday to you','Happy birthday dear Einstein'],
        ['Happy birthday to you']
      ]

reps = [ [2, 1],
     [1]
   ]

new_data = [[i for b in [[c]*d for c, d in zip(a, b)] for i in b] for a, b in zip(phrases, reps)]

Output:

[
['Happy birthday to you', 'Happy birthday to you', 'Happy birthday dear Einstein'], 
['Happy birthday to you']
]

Related Questions

Sponsored Content

39 Answered Questions

[SOLVED] How to make a flat list out of list of lists?

25 Answered Questions

[SOLVED] How can I safely create a nested directory in Python?

22 Answered Questions

[SOLVED] How do I list all files of a directory?

  • 2010-07-08 19:31:22
  • duhhunjonn
  • 2796942 View
  • 3123 Score
  • 22 Answer
  • Tags:   python directory

19 Answered Questions

[SOLVED] How to clone or copy a list?

28 Answered Questions

[SOLVED] Finding the index of an item given a list containing it in Python

  • 2008-10-07 01:39:38
  • Eugene M
  • 2936660 View
  • 2435 Score
  • 28 Answer
  • Tags:   python list

11 Answered Questions

14 Answered Questions

[SOLVED] Getting the last element of a list in Python

  • 2009-05-30 19:28:53
  • Janusz
  • 1426411 View
  • 1525 Score
  • 14 Answer
  • Tags:   python list indexing

33 Answered Questions

[SOLVED] How do I check if a list is empty?

  • 2008-09-10 06:20:11
  • Ray Vega
  • 1956976 View
  • 2969 Score
  • 33 Answer
  • Tags:   python list

6 Answered Questions

[SOLVED] How to get the number of elements in a list in Python?

  • 2009-11-11 00:30:54
  • y2k
  • 2811185 View
  • 1670 Score
  • 6 Answer
  • Tags:   python list

24 Answered Questions

[SOLVED] Difference between append vs. extend list methods in Python

Sponsored Content