I am writing some cryptographic algorithm using Python, but I have never worked with Python before.
First of all, look at this code then I'd explain the issue,
x = bytearray(salt[16:])
y = bytearray(sha_512[32:48])
c = [ i ^ j for i, j in zip( x, y ) ]
The value of x and y are ,
bytearray(b'AB\xc8s\x0eYzr2n\xe7\x06\x93\x07\xe2;')
bytearray(b'+q\xd4oR\x94q\xf7\x81vN\xfcz/\xa5\x8b')
I couldn't understand the third line of the code. In order to understand the third line, I had to look into the function zip()
,
I came across this question,
zip function help with tuples
According to answer in this question, the code,
zip((1,2,3),(10,20,30),(100,200,300))
will output,
[(1, 10, 100), (2, 20, 200), (3, 30, 300)]
but when I am trying to print it,
print(zip((1,2,3),(10,20,30),(100,200,300)))
I am getting this output,
<zip object at 0x0000000001C86108>
Why my output is different from the original output ?
Passing Arguments of Unequal Length Since 5 is the length of the first (and shortest) range() object, zip() outputs a list of five tuples.
If multiple iterables are passed, zip() returns an iterator of tuples with each tuple having elements from all the iterables. Suppose, two iterables are passed to zip() ; one iterable containing three and other containing five elements. Then, the returned iterator will contain three tuples.
The Python zip() function makes it easy to also zip more than two lists. This works exactly like you'd expect, meaning you just only need to pass in the lists as different arguments.
Use zip() Function to Zip Two Lists in Python The zip() function can take any iterable as its argument. It's used to return a zip object which is also an iterator. The returned iterator is returned as a tuple like a list, a dictionary, or a set. In this tuple, the first elements of both iterables are paired together.
In Python 3 zip
returns an iterator, use list
to see its content:
>>> list(zip((1,2,3),(10,20,30),(100,200,300)))
[(1, 10, 100), (2, 20, 200), (3, 30, 300)]
c = [ i ^ j for i, j in zip( x, y ) ]
is a list comprehension, in this you're iterating over the items returned from zip
and doing some operation on them to create a new list.
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