0

I was reading the following example from geeksforgeeks:

# Python code to demonstrate the working of  
# zip() 
  
# initializing lists 
name = [ "Manjeet", "Nikhil", "Shambhavi", "Astha" ] 
roll_no = [ 4, 1, 3, 2 ] 
marks = [ 40, 50, 60, 70 ] 
  
# using zip() to map values 
mapped = zip(name, roll_no, marks) 
  
# converting values to print as set 
mapped = set(mapped) 
  
# printing resultant values  
print ("The zipped result is : ",end="") 
print (mapped) 

but if you see the result:

The zipped result is : {('Shambhavi', 3, 60), ('Astha', 2, 70), ('Manjeet', 4, 40), ('Nikhil', 1, 50)}

I would have expected to see {('Manjeet', 4, 40), ('Nikhil', 1, 50), ('Shambhavi', 3, 60), ('Astha', 2, 70)}. So this made me thing if I want to do a mathematical operation between two lists by using zip, will zip itself change the order? I tried this little code, but it seems it doesn't, but still, I have the doubt. Did I just have luck this time or do I have to worry about it? I really need that the position of the couples in (A,B) do not change.

A = range(1,14)
B = range(2,15)

data = [x + y for x, y in zip(A, B)]
print(data)
Learning from masters
  • 2,032
  • 3
  • 29
  • 42

2 Answers2

5

zip makes use of the underlying iterators. It doesn't change the order.

Here is the doc

The left-to-right evaluation order of the iterables is guaranteed. This makes possible an idiom for clustering a data series into n-length groups using zip(*[iter(s)]*n)

Balaji Ambresh
  • 4,977
  • 2
  • 5
  • 17
4

Zip does not change the order of the objects passed to it. However set does, as it forms an unordered set from the zip results.

Alex S
  • 231
  • 1
  • 10
  • Thank you. i was a bit too quick on that one – Alex S Jul 24 '20 at 10:02
  • 2
    Originally set was actually implemented in Cpython as a dict with dummy values, but this is no longer the case and the implementations of set and dict have diverged – Chris_Rands Jul 24 '20 at 10:03
  • Thanks! I wasn't sure and this is one of the silly things that can introduce bugs to your mental idea... – Learning from masters Jul 24 '20 at 10:05
  • @Chris_Rands out of historical curiosity, do you know what version? – juanpa.arrivillaga Jul 24 '20 at 10:06
  • @juanpa.arrivillaga don't know- i think i read about it first here https://stackoverflow.com/questions/3949310/how-is-set-implemented#comment4218940_3949350 – Chris_Rands Jul 24 '20 at 10:08
  • The change to `dict` that makes it remember insertion order (while also being more efficient) is from [3.6](https://www.youtube.com/watch?v=p33CVV29OG8). I assume that `set` became basically the *old* "dict with dummy values", except presumably optimized so as not to allocate the unneeded space for the "dummy values". – Karl Knechtel Jul 24 '20 at 10:11
  • 2
    @KarlKnechtel No, that isn't how it happend. I'm pretty sure they were different implementations at 3.5 for sure, I'm pretty sure since at least python 3. dont recall paying attention to that since then. Fundamentally, because python dicts are everywhere even in the interpreter runtime itself, and sets are essentially specialized for narrower use-cases, I think they've had different implementations for a while. – juanpa.arrivillaga Jul 24 '20 at 10:15
  • 1
    @KarlKnechtel It long predates Python 3.6, the comment I linked to is from 2010- I think the optimisation pre-dates Python 3 – Chris_Rands Jul 24 '20 at 13:44