Something I seem to come across moderately often in my travels in the land of python is having a sequence of key value pairs that need to be converted into a dictionary. It seems to be the case that such patterns are found during scraping of websites and the like.
The usual pattern used is something this; this method, although it works, is not very helpful if you have an iterator and want to keep the code clean; this method only works with things that can be sliced, and iterators cannot.
This version is both useful and a little clever; it may not be immediately obvious how it works unless you are familiar with iterators.
Iterators in Python are consumable once; this means that once a value has been consumed from an iterator, you must obtain a new copy of the iterator if you wish to access said value again. This also means that you can repeatedly consume and get each value in the sequence.
The
function works here because for each of its arguments, it iterates from the first to last, getting one value from each, and bundling them into a 1
zip
, so you end up with something like this; 1
tuple
. This is then passed to the 1
[('key1', 'value1'), ('key2', 'value2')]
function where it deconstructs this (iterable, mind you, from 1
dict
) and builds a dictionary using the given values.1
zip