Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python list comprehension, unpacking and multiple operations

I want to unpack the tuples I create by doing the following so he the result is just one simple list. I can get the desired result in 2-3 lines but surely there is a oneliner list.comp?

x = range(10) y = [(i,j**2) for i,j in zip(x,x)] >>>y [(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25), (6, 36), (7, 49), (8, 64), (9 , 81)] >>> 

What I want is result = [0,0,1,1,2,4,3,9.....]

Doing

y = len(x)*[0] y[::2] = x y[1::2] = [i**2 for i in x] 

Gives what I want but what if I need the more general case:

y = [(i, sqrt(i), i**3, some_operation_on_i, f(i), g(i)) for i in x] 

Eg I should be able to get a straight list like result where I only specified one operation (square) to follow each i but now with an arbitrary number of operations following each i.

like image 740
arynaq Avatar asked Dec 19 '12 18:12

arynaq


People also ask

Does unpacking work with lists Python?

Unpacking assigns elements of the list to multiple variables. Use the asterisk (*) in front of a variable like this *variable_name to pack the leftover elements of a list into another list.

What is list unpacking in Python?

Unpacking in Python refers to an operation that consists of assigning an iterable of values to a tuple (or list ) of variables in a single assignment statement. As a complement, the term packing can be used when we collect several values in a single variable using the iterable unpacking operator, * .

How much faster is list comprehension than for loop Python?

As we can see, the for loop is slower than the list comprehension (9.9 seconds vs. 8.2 seconds). List comprehensions are faster than for loops to create lists. But, this is because we are creating a list by appending new elements to it at each iteration.

Which is faster lambda or list comprehension?

Actually, list comprehension is much clearer and faster than filter+lambda, but you can use whichever you find easier. The first thing is the function call overhead: as soon as you use a Python function (whether created by def or lambda) it is likely that the filter will be slower than the list comprehension.


2 Answers

>>> import itertools >>> list(itertools.chain.from_iterable(y)) [0, 0, 1, 1, 2, 4, 3, 9, 4, 16, 5, 25, 6, 36, 7, 49, 8, 64, 9, 81] 
like image 40
Tim Pietzcker Avatar answered Sep 18 '22 15:09

Tim Pietzcker


Use a nested list comprehension:

result = [a for tup in y for a in tup] 

Example:

>>> x = range(10) >>> y = [(i,j**2) for i,j in zip(x,x)] >>> [a for tup in y for a in tup] [0, 0, 1, 1, 2, 4, 3, 9, 4, 16, 5, 25, 6, 36, 7, 49, 8, 64, 9, 81] 

This will work fine for your more general case as well, or you could do it all in one step:

y = [a for i in x for a in (i, sqrt(i), i**3, some_operation_on_i, f(i), g(i))] 

In case the nested list comprehensions look odd, here is how this would look as a normal for loop:

y = [] for i in x:     for a in (i, sqrt(i), i**3, some_operation_on_i, f(i), g(i)):         y.append(a) 
like image 198
Andrew Clark Avatar answered Sep 22 '22 15:09

Andrew Clark