How do I transpose a List? [duplicate] - python

This question already has answers here:
Transpose list of lists
(14 answers)
Closed 8 years ago.
Let's say I have a SINGLE list [[1,2,3],[4,5,6]]
How do I transpose them so they will be: [[1, 4], [2, 5], [3, 6]]?
Do I have to use the zip function? Is the zip function the easiest way?
def m_transpose(m):
trans = zip(m)
return trans

Using zip and *splat is the easiest way in pure Python.
>>> list_ = [[1,2,3],[4,5,6]]
>>> zip(*list_)
[(1, 4), (2, 5), (3, 6)]
Note that you get tuples inside instead of lists. If you need the lists, use map(list, zip(*l)).
If you're open to using numpy instead of a list of lists, then using the .T attribute is even easier:
>>> import numpy as np
>>> a = np.array([[1,2,3],[4,5,6]])
>>> print(*a)
[1 2 3] [4 5 6]
>>> print(*a.T)
[1 4] [2 5] [3 6]

The exact way of use zip() and get what you want is:
>>> l = [[1,2,3],[4,5,6]]
>>> [list(x) for x in zip(*l)]
>>> [[1, 4], [2, 5], [3, 6]]
This code use list keyword for casting the tuples returned by zip into lists.

You can use map with None as the first parameter:
>>> li=[[1,2,3],[4,5,6]]
>>> map(None, *li)
[(1, 4), (2, 5), (3, 6)]
Unlike zip it works on uneven lists:
>>> li=[[1,2,3],[4,5,6,7]]
>>> map(None, *li)
[(1, 4), (2, 5), (3, 6), (None, 7)]
>>> zip(*li)
[(1, 4), (2, 5), (3, 6)]
# ^^ 7 missing...
Then call map again with list as the first parameter if you want the sub elements to be lists instead of tuples:
>>> map(list, map(None, *li))
[[1, 4], [2, 5], [3, 6]]
(Note: the use of map with None to transpose a matrix is not supported in Python 3.x. Use zip_longest from itertools to get the same functionality...)

zip() doesn't seem to do what you wanted, using zip() you get a list of tuples. This should work though:
>>> new_list = []
>>> old_list = [[1,2,3],[4,5,6]]
>>> for index in range(len(old_list[0])):
... new_list.append([old_list[0][index], old_list[1][index]])
...
>>> new_list
[[1, 4], [2, 5], [3, 6]]

Related

python how to find unique pairs from 2 lists [duplicate]

This question already has answers here:
How to get the cartesian product of multiple lists
(17 answers)
Closed 10 months ago.
I have the following list of lists:
data = [[1,2,3],[4,5,6]]
and I want to generate something like the following:
[1,4]
[1,5]
[1,6]
[2,4]
[2,5]
[2,6]
[3,4]
[3,5]
[3,6]
What can produce the expected result? To clarify, I'm looking for all unique pairs of 1 item from each of the two sublists.
import itertools
data = [[1,2,3],[4,5,6]]
print(list(itertools.product(*data)))
Output:
[(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6)]
You can use itertools.product() with list unpacking to generate tuples containing one element from each sublist. You can then use map() to transform each of those tuples to lists.
from itertools import product
list(map(list, product(*data)))
This outputs:
[[1, 4], [1, 5], [1, 6], [2, 4], [2, 5], [2, 6], [3, 4], [3, 5], [3, 6]]
result = []
for x in data[0]:
for y in data[1]:
result.append([x, y])
or one line:
[(x,y) for x in data[0] for y in data[1]]
that will give you a list of tuples
list of lists:
list(map(list, [(x,y) for x in data[0] for y in data[1]]))

How to combine two list into one list element wise? [duplicate]

This question already has answers here:
How to merge lists into a list of tuples?
(10 answers)
Closed 2 years ago.
For example, if I have:
A = [1, 2, 3]` & `B = [4, 5, 6]
and I would like to have:
C = [[1, 4], [2, 5], [3, 6]]
You can use tuple and zip to meet this requirement.
Sample code -
>>> a = [1,2,3]
>>> b = [4,5,6]
>>> c = tuple(zip(a,b))
>>> print(c)
((1, 4), (2, 5), (3, 6))
There is a builtin function called zip for this:
[list(ab) for ab in zip(a,b)]
Or using map and zip:
list(map(list, zip(a,b)))
Both return:
[[1, 4], [2, 5], [3, 6]]
You can do this:
a = [1,2,3]
b = [4,5,6]
c = [list(x) for x in zip(a, b)]
In [110]: A = [1,2,3]
In [111]: B = [4,5,6]
In [112]: list(zip(A,B))
Out[112]: [(1, 4), (2, 5), (3, 6)]

Getting the mode from tuples inside lists [duplicate]

How do I convert
[(1,), (2,), (3,)]
to
[1, 2, 3]
Using simple list comprehension:
e = [(1,), (2,), (3,)]
[i[0] for i in e]
will give you:
[1, 2, 3]
#Levon's solution works perfectly for your case.
As a side note, if you have variable number of elements in the tuples, you can also use chain from itertools.
>>> a = [(1, ), (2, 3), (4, 5, 6)]
>>> from itertools import chain
>>> list(chain(a))
[(1,), (2, 3), (4, 5, 6)]
>>> list(chain(*a))
[1, 2, 3, 4, 5, 6]
>>> list(chain.from_iterable(a)) # More efficient version than unpacking
[1, 2, 3, 4, 5, 6]
Here is another alternative if you can have a variable number of elements in the tuples:
>>> a = [(1,), (2, 3), (4, 5, 6)]
>>> [x for t in a for x in t]
[1, 2, 3, 4, 5, 6]
This is basically just a shortened form of the following loops:
result = []
for t in a:
for x in t:
result.append(x)
>>> a = [(1,), (2,), (3,)]
>>> zip(*a)[0]
(1, 2, 3)
For a list:
>>> list(zip(*a)[0])
[1, 2, 3]
>>> a = [(1,), (2,), (3,)]
>>> b = map(lambda x: x[0], a)
>>> b
[1, 2, 3]
With python3, you have to put the list(..) function to the output of map(..), i.e.
b = list(map(lambda x: x[0], a))
This is the best solution in one line using python built-in functions.
You can also use sum function as follows:
e = [(1,), (2,), (3,)]
e_list = list(sum(e, ()))
And it also works with list of lists to convert it into a single list, but you will need to use it as follow:
e = [[1, 2], [3, 4], [5, 6]]
e_list = list(sum(e, []))
This will give you [1, 2, 3, 4, 5, 6]
There's always a way to extract a list from another list by ...for...in.... In this case it would be:
[i[0] for i in e]
Using operator or sum
>>> from functools import reduce ### If python 3
>>> import operator
>>> a = [(1,), (2,), (3,)]
>>> list(reduce(operator.concat, a))
[1, 2, 3]
(OR)
>>> list(sum(a,()))
[1, 2, 3]
>>>
If in python > 3 please do the import of reduce from functools
like from functools import reduce
https://docs.python.org/3/library/functools.html#functools.reduce
You can also unpack the tuple in the list comprehension:
e = [(1,), (2,), (3,)]
[i for (i,) in e]
will still give:
[1, 2, 3]
One Liner yo!
list(*zip(*[(1,), (2,), (3,)]))
In these situations I like to do:
a = [(1,), (2,), (3,)]
new_a = [element for tup in a for element in tup]
This works even if your tuples have more than one element. This is equivalent to doing this:
a = [(1,), (2,), (3,)]
new_a = []
for tup in a:
for element in tup:
new_a.append(element)
If it is already a numpy array, use ravel() method which is more faster than list comprehension.
If it is already a list, list comprehension is better.
Most of the answers above only prints the first element not all the elements
For numpy arrays
#arr = np.array([(1,2), (2,3), (3,4)])
#faster than list comprehension
arr.ravel().tolist()
#output => [1,2,2,3,3,4]
For list
list_ = [(1,2), (2,3), (3,4)]
[x for y in list_ for x in y]
#output => [1,2,2,3,3,4]

Pairwise appending in python without ZIP [duplicate]

This question already has answers here:
How to merge lists into a list of tuples?
(10 answers)
Closed 7 years ago.
I am currently learning list comprehension in Python. How would I do the following:
l1 = [2,4,6,8]
l2 = [2,3,4,5]
l = [*some list comprehension*]
so that
l = [[2,2],[4,3],[6,4],[8,5]]
EDIT: Can I do this without zip?
You want the zip function.
Example -
>>> l1 = [2,4,6,8]
>>> l2 = [2,3,4,5]
>>>
>>> l = list(zip(l1,l2))
>>> l
[(2, 2), (4, 3), (6, 4), (8, 5)]
If you want the inner lists to be of type list instead of tuple -
>>> l = [list(x) for x in zip(l1,l2)]
>>> l
[[2, 2], [4, 3], [6, 4], [8, 5]]
In python 3.x, zip returns an iterator, so if you do not want a list, but just want to iterate over each combined (zipped) element, you can just directly use - zip(l1,l2) .
As it is asked in the question, to do it without zip function, you can use enumerate function to get the index as well as the element from one list and then use the index to get the element from second list.
>>> l = [[x,l2[i]] for i,x in enumerate(l1)]
>>> l
[[2, 2], [4, 3], [6, 4], [8, 5]]
But this would not work unless both lists have same size.Also not sure why you would want to do it without zip .
Using list comprehension and zip:
>>> l1 = [2, 4, 6, 8]
>>> l2 = [2, 3, 4, 5]
>>> [[x, y] for x, y in zip(l1, l2)]
[[2, 2], [4, 3], [6, 4], [8, 5]]
You can use zip as
>>> l1 = [2,4,6,8]
>>> l2 = [2,3,4,5]
>>> zip(l1,l2)
[(2, 2), (4, 3), (6, 4), (8, 5)]
>>> [ list(x) for x in zip(l1,l2) ]
[[2, 2], [4, 3], [6, 4], [8, 5]]

How to merge lists into a list of tuples?

What is the Pythonic approach to achieve the following?
# Original lists:
list_a = [1, 2, 3, 4]
list_b = [5, 6, 7, 8]
# List of tuples from 'list_a' and 'list_b':
list_c = [(1,5), (2,6), (3,7), (4,8)]
Each member of list_c is a tuple, whose first member is from list_a and the second is from list_b.
In Python 2:
>>> list_a = [1, 2, 3, 4]
>>> list_b = [5, 6, 7, 8]
>>> zip(list_a, list_b)
[(1, 5), (2, 6), (3, 7), (4, 8)]
In Python 3:
>>> list_a = [1, 2, 3, 4]
>>> list_b = [5, 6, 7, 8]
>>> list(zip(list_a, list_b))
[(1, 5), (2, 6), (3, 7), (4, 8)]
In python 3.0 zip returns a zip object. You can get a list out of it by calling list(zip(a, b)).
You can use map lambda
a = [2,3,4]
b = [5,6,7]
c = map(lambda x,y:(x,y),a,b)
This will also work if there lengths of original lists do not match
Youre looking for the builtin function zip.
I am not sure if this a pythonic way or not but this seems simple if both lists have the same number of elements :
list_a = [1, 2, 3, 4]
list_b = [5, 6, 7, 8]
list_c=[(list_a[i],list_b[i]) for i in range(0,len(list_a))]
The output which you showed in problem statement is not the tuple but list
list_c = [(1,5), (2,6), (3,7), (4,8)]
check for
type(list_c)
considering you want the result as tuple out of list_a and list_b, do
tuple(zip(list_a,list_b))
I know this is an old question and was already answered, but for some reason, I still wanna post this alternative solution. I know it's easy to just find out which built-in function does the "magic" you need, but it doesn't hurt to know you can do it by yourself.
>>> list_1 = ['Ace', 'King']
>>> list_2 = ['Spades', 'Clubs', 'Diamonds']
>>> deck = []
>>> for i in range(max((len(list_1),len(list_2)))):
while True:
try:
card = (list_1[i],list_2[i])
except IndexError:
if len(list_1)>len(list_2):
list_2.append('')
card = (list_1[i],list_2[i])
elif len(list_1)<len(list_2):
list_1.append('')
card = (list_1[i], list_2[i])
continue
deck.append(card)
break
>>>
>>> #and the result should be:
>>> print deck
>>> [('Ace', 'Spades'), ('King', 'Clubs'), ('', 'Diamonds')]
Or map with unpacking:
>>> list(map(lambda *x: x, list_a, list_b))
[(1, 5), (2, 6), (3, 7), (4, 8)]
>>>
One alternative without using zip:
list_c = [(p1, p2) for idx1, p1 in enumerate(list_a) for idx2, p2 in enumerate(list_b) if idx1==idx2]
In case one wants to get not only tuples 1st with 1st, 2nd with 2nd... but all possible combinations of the 2 lists, that would be done with
list_d = [(p1, p2) for p1 in list_a for p2 in list_b]
Like me, if anyone needs to convert it to list of lists (2D lists) instead of list of tuples, then you could do the following:
list(map(list, list(zip(list_a, list_b))))
It should return a 2D List as follows:
[[1, 5],
[2, 6],
[3, 7],
[4, 8]]

Categories

Resources