Unpacking tuples in a python list comprehension (cannot use the *-operator)

You can’t use * iterable unpacking in a list comprehension, that syntax is only available in calls, and in Python 3, when using assignments.

If you want to use a list comprehension, just put your for loops in series; you do want to access the values from my_list directly rather than generate indices though:

[v for v in my_list for _ in range(3)]

There are several other options too:

  • Use itertools.repeat() to repeat the values, and itertools.chain.from_iterable() to concatenate those iterators back together: chain.from_iterable(repeat(v, 3) for v in my_list). That produces an iterator, you can use list() on it to get a list again.
  • Use chain.from_iterable(zip(*([my_list] * 3))) to create an iterator over the repeated values (the repeated list is transposed into repeated columns). This is not nearly as readable, of course, but we could make it slightly less bad by using repeat() here too: chain.from_iterable(zip(*repeat(my_list, 3))). Again, use list() to produce the same output as the list comprehension.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)