Reading the Python documentation and there are a few ways of creating dictionaries.
dict()
dict(**kwargs)
dict(mapping, **kwargs)
dict(iterable, **kwargs)
https://docs.python.org/2/library/stdtypes.html (5.8 Mapping types)
I don't understand the difference between mapping and iterable - the documentation reads:
If a positional argument is given and it is a mapping object, a dictionary is created with the same key-value pairs as the mapping object. Otherwise, the positional argument must be an iterable object. Each item in the iterable must itself be an iterable with exactly two objects.
It seems to me that mapping and iterable are the same thing here... Can you please help me to understand the difference?
I don't understand the difference between mapping and iterable
A mapping is a collection of key/value pairs that allow key access to value - it "maps" keys to values. The most obvious mapping builtin type is the dict.
An iterable is an object that can be iterated - which basically means you can use it in a for obj in iterable: statement. This include sequence types (strings, lists, etc) as well as quite a few other (files, dbapi cursors, generators etc), and, well, dicts too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With