Given a list of 0 to N sequential integers, where N is an even number, e.g. A = [0 , 1 , 2 , 3 , 4] I’m looking for a list comprehension, i.e. of type [a for a in A], for creating another list B = [0 , 1 , 1 , 2 , 3 , 3 , 4] that repeats the odd numbers in the input list, A.
Given a list of 0 to N sequential integers, where N is an even
Share
Here’s a (somewhat ugly) solution with a list comprehension:
The list comprehension builds a list of sublists:
The
itertools.chaincall is just one of the many ways to flatten a list in Python.