Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python limit of the * operator and zip() function

Tags:

python

memory

zip

I have a Python code similar to this one:

for lines in zip(*files):
   # do something

where files is a list of files, each file is a list of lines and each line is a list of strings. Therefore, the code above should first unpack the list files and then apply the function zip(), returning a tuple with the first line of each file. The problem is that this works just fine, if the length of the list of files is 30 (for example). However, if the lenght is bigger, for instance, 120, the code inside the loop doesn't get executed even once.

The conclusion is that either the zip() function is returning an empty list or the * operator is not doing its job. Either way, my question is if there is a limit in the arguments that zip() can handle (or * can unpack) or it is somehow limited by the amount of memory that my computer has, since I haven't been able to find anything in Python's documentation.

PS: I'm running Python 2.4

like image 212
skd Avatar asked Mar 03 '11 10:03

skd


People also ask

What does * do in zip Python?

Unzip Values in Python If you have a list of tuples—or zipped values—that you want to divide, you can use the zip() function's unpacking operator. This is an asterisk * used in conjunction with the zip() function.

What is the role of the * operator within the zip () function?

Basically, it passes the contents of the lists as arguments.

What does zip and * operator do?

The "*" operator unpacks a list and applies it to a function. The zip function takes n lists and creates n-tuple pairs from each element from both lists: zip([iterable, ...]) This function returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables.

What is the time complexity of zip function in Python?

In python 3. x, the zip function itself runs in O(1) time, as it just allocates a special iterable (called the zip object), and assigns the parameter array to an internal field. The function invocation itself (before control reaches in zip) is O(N) , as the interpreter must convert the parameters to an array.


1 Answers

If one of the files is empty, zip will return an empty list. As of Python 2.6 you can use itertools.izip_longest to handle that. On older versions, you can use map(None, *files), courtesy of @Sven Marnach.

like image 138
Björn Pollex Avatar answered Sep 18 '22 12:09

Björn Pollex