1

So, let's say I have this structure:

list_test = [["A", "1", "Test"], ["B", "2", "Test2"], ["C", "3", "Test"]]

my desired output is:

[["A", "1", "Test"], ["A", "1", "Test"], ["A", "1", "Test"], ["B", "2", "Test2"], ["B", "2", "Test2"], ["B", "2", "Test2"], ["C", "3", "Test"], ["C", "3", "Test"], ["C", "3", "Test"]]

So I'm multiplying 3 times each list. I'm able to do it with extend and other ways of iteration with no issues, but I'm trying to learn solving it using list comprehensions.

I thought about something like this: [i * 4 for i in list_test] but that copies the list inside of itself, like this:

[['A', '1', 'Test', 'A', '1', 'Test', 'A', '1', 'Test'], ['B', '2', 'Test2', 'B', '2', 'Test2', 'B', '2', 'Test2'], ['C', '3', 'Test', 'C', '3', 'Test', 'C', '3', 'Test']]

[[i] * 4 for i in list_test] does something similar to what I want but it's inside another list, that's not what I want it.

Is it possible to solve this using comprehension?

Adam Smith
  • 52,157
  • 12
  • 73
  • 112
IgnacioDLF
  • 40
  • 7

3 Answers3

4
[ entry for entry in list_test for _ in range(3) ]

should do the trick

SamudNeb
  • 211
  • 2
  • 6
3

You could handle this by using two loops in your comprehension. Consider the trivial case:

given = [['a']]
want = [['a'], ['a'], ['a'], ['a']]

With this you could do:

got = [given[0] for _ in range(3)]

However your example is a little more complex, as you have multiple elements. You can instead do:

got = [sublst for sublst in given for _ in range(3)]
Adam Smith
  • 52,157
  • 12
  • 73
  • 112
  • N.B. that the `_` has no special meaning here, it's just used to idiomatically indicate that we need an identifier here for the grammar, but we don't ever care about this value. – Adam Smith Sep 29 '20 at 03:23
  • 1
    Thanks. Extra credit for the comment, I'm still trying to wrap my head around the last for, that comment about the underscore is helpful. I marked this as the best answer because of that, thanks to SamudNeb that was also spot on too at the same time. – IgnacioDLF Sep 29 '20 at 03:35
  • 1
    @IgnacioDLF this [other answer of mine](https://stackoverflow.com/questions/20639180/explanation-of-how-nested-list-comprehension-works/20639516#20639516) might help! – Adam Smith Sep 29 '20 at 03:40
  • It does help a lot, thanks. I think I get the other one. What would be the equivalent to a function using 'for' for this "got = [sublst for sublst in given for _ in range(3)]" Sorry if it's too much trouble explain. – IgnacioDLF Sep 29 '20 at 04:19
  • 1
    `for sublst in given: for _ in range(3): ... ` – Adam Smith Sep 29 '20 at 04:43
1

The way I'd do this is with a simple generator and a comprehension:

list_test = [["A", "1", "Test"], ["B", "2", "Test2"], ["C", "3", "Test"]]


def list_multiplier(list, n):
    for element in list:
        for i in range(n):
            yield element


list_result = [e for e in list_multiplier(list_test, 3)]
print(list_result)

Result:

[['A', '1', 'Test'], ['A', '1', 'Test'], ['A', '1', 'Test'], ['B', '2', 'Test2'], ['B', '2', 'Test2'], ['B', '2', 'Test2'], ['C', '3', 'Test'], ['C', '3', 'Test'], ['C', '3', 'Test']]
CryptoFool
  • 21,719
  • 5
  • 26
  • 44