Skip to content Skip to sidebar Skip to footer

Python: What Is Zip Doing In This List Comprehension

I am trying to understand this: a = 'hello' b = 'world' [chr(ord(x) ^ ord(y)) for (x, y) in zip(a[:len(b)], b)] I understand the XOR part but I don't get what zip is doing.

Solution 1:

zip combines each letter of a and b together.

a = "hello"
b = "world"
print zip(a, b)
>>>
    [('h', 'w'), ('e', 'o'), ('l', 'r'), ('l', 'l'), ('o', 'd')]

Solution 2:

It isn't doing anything out of the ordinary for zip.

The list slicing of a is overkill since zip assumes this behavior.

As stated in the docs:

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.


Post a Comment for "Python: What Is Zip Doing In This List Comprehension"