-1

I see examples on Hugging face where they are zipping dictionaries with list (one such example below):

batch = {'input_ids': [[1,2,3], [4,5,6]], 'attention_mask': [[1,1,1], [0,0,0]], 'labels': [[10, 11, 12], [13, 14, 15]]}
def insert_random_mask(batch):
    features = [dict(zip(batch, t)) for t in zip(*batch.values())]
    return features

print(insert_random_mask(batch)
>>> [{'input_ids': [1, 2, 3], 'attention_mask': [1, 1, 1], 'labels': [10, 11, 12]}, {'input_ids': [4, 5, 6], 'attention_mask': [0, 0, 0], 'labels': [13, 14, 15]}]

I also see SO posts on the same too: Zipping a Python Dictionary and List together

But dictionaries do not provide ordering. How is it ok to zip an (unordered) dictionary with an ordered list?

Edit: it is not about iterability of dict, it is more about mapping the right keys with right values. In the example about if ordering is not guaranteed, how can you be sure that you are mapping the right values with the keys

PS1
  • 179
  • 1
  • 1
  • 7
  • 5
    #1, as of python 3.6, dictionaries are ordered. #2 "does not guarantee ordering" has nothing to do with iterability – Mad Physicist Aug 25 '23 at 14:06
  • 2
    You say: `"dictionaries do not provide ordering"`. You will have to provide evidence for such an assertion. – quamrana Aug 25 '23 at 14:07
  • If the ordering is not guaranteed, then the zip might match keys with different values – PS1 Aug 25 '23 at 14:08
  • Also, notice that the zip is over the keys and values. Those are guaranteed to iterate in the same order for a given dictionary – Mad Physicist Aug 25 '23 at 14:08
  • @PS1, just because an operation has undefined behavior doesn't mean it's the language's job to disallow it, _particularly_ when that behavior is not a primitive itself but the result of combining primitives, each of which itself works as designed and documented. – Charles Duffy Aug 25 '23 at 14:16
  • 3
    @quamrana - prior to python 3.6 dict did not guarantee order and it would have sounded bizarre to somehow prove that every time it was mentioned. Generally, an order preserving hash table is uncommon. When added the documentation said _The order-preserving aspect of this new implementation is considered an implementation detail and should not be relied upon_ So its reasonable not to know this. – tdelaney Aug 25 '23 at 14:17
  • Taking things that individually work and then making them break when used together is the kind of unintuitive behavior that would violate "special cases aren't special enough to break the rules", not to mention "simple is better than complex". That kind of thing is better suited for static checkers than the language runtime itself. – Charles Duffy Aug 25 '23 at 14:18
  • @tdelaney Python 2.7 already guaranteed corresponding order (don't know about older versions). – Kelly Bundy Aug 25 '23 at 14:24
  • Finally: Even when ordering isn't _well-defined_, that doesn't mean it isn't _consistent_. In a naive tree your ordering is by hash of the keys; that can change in hard-to-predict ways (if, say, that hash is salted to prevent denial-of-service attacks), but iterating over the same dictionary twice within the same running copy of the program, you'll generally get the same result. Like anything else that isn't a documented language guarantee it's playing with fire, but folks do it without burning themselves. – Charles Duffy Aug 25 '23 at 14:24
  • See also https://stackoverflow.com/questions/47172349/what-ordering-does-dict-keys-and-dict-values-guarantee (can’t append to the dupe list right now). – MisterMiyagi Aug 25 '23 at 14:26
  • 1
    @tdelaney: I think I understand the facts of what you've written, but since python 3.6 was released in 2016 (its now 2023 for the curious) and has reached the end of support, it seems curious to now assert that *dictionaries do not provide ordering* without some sort of reason for the statement. – quamrana Aug 25 '23 at 14:26
  • 1
    @quamrana, ...as an opposing view: I have a lot of respect for people who treat things that are documented as not being language guarantees as if they don't exist; from a portability and futureproofing perspective it's the Right Thing. – Charles Duffy Aug 25 '23 at 14:27
  • 1
    Do the answers to this [question](https://stackoverflow.com/questions/39980323/are-dictionaries-ordered-in-python-3-6) help at all? – quamrana Aug 25 '23 at 14:33
  • 1
    Python dicts have been ordered from 3.6 to 3.11 (present day) ... by this stage we can assume it's here to stay, since enough code will have been written which expects the behaviour that removing it will break things. Current [docs](https://docs.python.org/3.11/tutorial/datastructures.html#dictionaries) say _"Performing `list(d)` on a dictionary returns a list of all the keys used in the dictionary, in insertion order"_ without further qualification – Anentropic Aug 25 '23 at 15:29

0 Answers0