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 string
s. 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
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.
Basically, it passes the contents of the lists as arguments.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With