How to Zip Lists in a List

How to zip lists in a list

Try this:

>>> zip(*[[1,2], [3,4], [5,6]])
[(1, 3, 5), (2, 4, 6)]

See Unpacking Argument Lists:

The reverse situation occurs when the arguments are already in a list or tuple but need to be unpacked for a function call requiring separate positional arguments. For instance, the built-in range() function expects separate start and stop arguments. If they are not available separately, write the function call with the *-operator to unpack the arguments out of a list or tuple:

>>> range(3, 6)             # normal call with separate arguments
[3, 4, 5]
>>> args = [3, 6]
>>> range(*args) # call with arguments unpacked from a list
[3, 4, 5]

Zip lists in Python

When you zip() together three lists containing 20 elements each, the result has twenty elements. Each element is a three-tuple.

See for yourself:

In [1]: a = b = c = range(20)

In [2]: zip(a, b, c)
Out[2]:
[(0, 0, 0),
(1, 1, 1),
...
(17, 17, 17),
(18, 18, 18),
(19, 19, 19)]

To find out how many elements each tuple contains, you could examine the length of the first element:

In [3]: result = zip(a, b, c)

In [4]: len(result[0])
Out[4]: 3

Of course, this won't work if the lists were empty to start with.

Any way to zip to list of lists?

You can use a comprehension:

listz = [list(i) for i in zip(listx, listy)]

or generator expression:

listz = (list(i) for i in zip(listx, listy))

How to zip the elements of a single nested list

You need to give the single sub-lists via unpacking (*) as single arguments to zip() like this:

d = [[1,2,3],[4,5,6]]          
zip(*d) # You need this one
[(1, 4), (2, 5), (3, 6)]

This even works for longer lists, in case this is the behaviour you want:

zip(*[[1,2,3],[4,5,6],[7,8,9]])
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]

If you want to have a list of lists instead of a list of tuples, just do this:

map(list, zip(*d))
[[1, 4], [2, 5], [3, 6]]

Efficient way to zip a list with lists nested in another list in python

Use * operator (See Python tutorial - Unpacking Argument Lists)

>>> a = [1,2,3]
>>> b = [ ['a', 'b', 'c'], ['1', '2', '3'] ]
>>> zip(a, *b)
[(1, 'a', '1'), (2, 'b', '2'), (3, 'c', '3')]

zip function in python for lists

The issue is that zip is not used to achieve merging your lists

Here is the correct implementation

list1 = []
list2 = [0]
result=list1+list2
print(result)

# result output: [0]

However, zip function is used as an iterator between lists. below is an example to help you with the concept

a = ("1", "2", "3")
b = ("A", "B", "C")

x = zip(a, b)

for i in x: # will loop through the items in x
print(i)

# result output:
# ('1', 'A')
# ('2', 'B')
# ('3', 'C')

How to zip a list of lists in python?

Your actual code is discarding lists. You only ever process the last entry.

Your code works fine otherwise. Just do that in the loop and then append the result to some final list:

results = []

for k in range(len(seq_list)):
column_list = [[] for i in range(len(seq_list[k][0]))]
for seq in seq_list[k]:
for i, nuc in enumerate(seq):
column_list[i].append(nuc)
# process `column_list` here, in the loop (no need to assign to ddd)
tt = ["".join(y for y in x if y in {'A','G','T','C'}) for x in column_list]

results.append(tt)

Note that you could use the zip() function instead of your transposition list:

results = []
for sequence in seq_list:
for column_list in zip(*sequence):
tt = [''.join([y for y in x if y in 'AGTC']) for x in column_list]
results.append(tt)

Zipping multiple lists together?

zip() takes a variable number of arguments. zip(one, two, three) will work, and so on for as many arguments as you wish to pass in.

>>> zip([1, 2, 3], "abc", [True, False, None])
[(1, 'a', True), (2, 'b', False), (3, 'c', None)]

If you have an unknown number of iterables (a list of them, for example), you can use the unpacking operator (*) to unpack (use as arguments) an iterable of iterables:

>>> iterables = [[1, 2, 3], "abc", [True, False, None]]
>>> zip(*iterables)
[(1, 'a', True), (2, 'b', False), (3, 'c', None)]

(This is often referred to as unzipping as it will reverse the operation - a == zip(*zip(a)))

How to combine the elements of two lists using zip function in python?

I believe the function you are looking for is itertools.product:

lasts = ['x', 'y', 'z']
firsts = ['a', 'b', 'c']

from itertools import product
for last, first in product(lasts, firsts):
print (last, first)

x a
x b
x c
y a
y b
y c
z a
z b
z c

Another alternative, that also produces an iterator is to use a nested comprehension:

iPairs=( (l,f) for l in lasts for f in firsts)
for last, first in iPairs:
print (last, first)

How to zip lists of different sizes?

It's not pretty, but this is what I came up with:

In [10]: a = [1, 3, 5, 7, 9, 11]
...: b = [2, 4, 6, 8]

In [11]: output = (tuple(l[i] for l in (a,b) if i < len(l)) for i, e in enumerate(max(a,b, key=len)))

In [12]: list(output)
Out[12]: [(1, 2), (3, 4), (5, 6), (7, 8), (9,), (11,)]

Make it a function:

from collections.abc import (Iterable, Iterator)
from itertools import count

def zip_staggered(*args: Iterable) -> Iterator[tuple]:
for i in count():
if (next_elem := tuple(a[i] for a in args if i < len(a))):
yield next_elem
else:
break


Related Topics



Leave a reply



Submit