Sufiyan Ghori
Sufiyan Ghori

Reputation: 18753

zip function giving incorrect output

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 ?

Upvotes: 8

Views: 8252

Answers (1)

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250941

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.

Upvotes: 16

Related Questions