|
|
|
|
|
by klyrs
2327 days ago
|
|
> Code that ordered the dictionary keys before iterating are now slightly innefficient due to extra work of sorting a sorted list. Dicts are ordered, specifically insertion-ordered, not sorted. Sometimes they'll be sorted: D = {i:i for i in range(10)}
... specifically, when you insert them in sorted order. But then you can break the sortedness on your next insert: D[-1] = -1
What it allows is parallel iteration: zip(D.keys(), D.values())
now being synonymous with D.items()
This enables, nay, encourages people to write code that is very subtly broken in 3.5 and below. |
|
> If items(), keys(), values(), iteritems(), iterkeys(), and itervalues() are called with no intervening modifications to the dictionary, the lists will directly correspond. This allows the creation of (value, key) pairs using zip(): pairs = zip(d.values(), d.keys()).