Initialising variables in Python: Dictionaries
Dictionary is the standard mapping type collection object available in Python. A mapping object is a mutable object that maps hashable values to arbitrary objects.
Get Aditya Garg’s stories in your inbox
Join Medium for free to get updates from this writer.
From https://docs.python.org/3/library/stdtypes.html#mapping-types-dict
A dictionary’s keys are almost arbitrary values. Values that are not hashable, that is, values containing lists, dictionaries or other mutable types (that are compared by value rather than by object identity) may not be used as keys. Values that compare equal (such as
1,1.0, andTrue) can be used interchangeably to index the same dictionary entry.class dict(**kwargs)
class dict(mapping, **kwargs)
class dict(iterable, **kwargs)
Return a new dictionary initialized from an optional positional argument and a possibly empty set of keyword arguments.
Dictionaries can be created by several means:
Use a comma-separated list of
key: valuepairs within braces:{'jack': 4098, 'sjoerd': 4127}or{4098: 'jack', 4127: 'sjoerd'}Use a dict comprehension:
{},{x: x ** 2 for x in range(10)}Use the type constructor:
dict(),dict([('foo', 100), ('bar', 200)]),dict(foo=100, bar=200)
Initialising Dictionaries
A dictionary can be initialised using key:value pairs, using range() function and using constructor dict().
## Dictionary using key:value pairs
dictionaryUsingKeyValuePairs1 = {'jack': 4098, 'sjoerd': 4127}
dictionaryUsingKeyValuePairs2 = {4098: 'jack', 4127: 'sjoerd'}
print("Dictionary Using Key:Value Pairs: ")
print(dictionaryUsingKeyValuePairs1)
print(dictionaryUsingKeyValuePairs2)
print()
## Dictionary using range() function
dictionaryUsingRange = {x: x ** 2 for x in range(10)}
print("Dictionary Using Range: ")
print(dictionaryUsingRange)
print()
## Dictionary using dict() constructor
dictionaryUsingConstructor1 = dict([('foo', 100), ('bar', 200)])
dictionaryUsingConstructor2 = dict(foo=100, bar=200)
dictionaryUsingConstructor3 = dict(zip(['foo', 'bar'], [100, 200]))
print("Dictionary Using Constructor: ")
print(dictionaryUsingConstructor1)
print(dictionaryUsingConstructor2)
print(dictionaryUsingConstructor3)
print()
## Dictionary using Key:Value pairs and constructor
dictionaryUsingKeyValueConstructor = dict({'foo': 100, 'bar': 200})
print("Dictionary Using Key:Value Pairs and Constructor: ")
print(dictionaryUsingKeyValueConstructor)
print()
## Example from Pythn Docs https://docs.python.org/3/library/stdtypes.html#mapping-types-dict
## The following examples all return a dictionary equal to {"one": 1, "two": 2, "three": 3}:
a = dict(one=1, two=2, three=3)
b = {'one': 1, 'two': 2, 'three': 3}
c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))
d = dict([('two', 2), ('one', 1), ('three', 3)])
e = dict({'three': 3, 'one': 1, 'two': 2})
f = dict({'one': 1, 'three': 3}, two=2)
print(a)
print(b)
print(c)
print(d)
print(e)
print(f)
print(a == b == c == d == e == f)Boolean Values of Dictionaries
Empty dictionaries have False value while non-empty dictionaries have True value.
Such Boolean values are called Falsy and Truthy values. [Please see Danesh Forouhari’s comment]
For more details, please see my post https://aditya-garg.medium.com/python-boolean-variables-and-functions-5ad63cacb89e or visit https://www.geeksforgeeks.org/truthy-vs-falsy-values-in-python/










