Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 924
Sufiyan Ghori Avatar asked Oct 09 '13 16:10

Sufiyan Ghori


People also ask

What is the output of zip function in Python?

Passing Arguments of Unequal Length Since 5 is the length of the first (and shortest) range() object, zip() outputs a list of five tuples.

Can you zip two 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.

Can you zip more than two lists Python?

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.

How do I zip two lists together?

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.


1 Answers

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.

like image 146
Ashwini Chaudhary Avatar answered Oct 02 '22 14:10

Ashwini Chaudhary