Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 7

Dictionaries

A dictionary is mutable and is another container type that can store any


number of Python objects, including other container types. • Dictionaries
consist of pairs (called items) of keys and their corresponding values. •
Python dictionaries are also known as associative arrays or hash tables. •
dict={'Alice':'2341','Beth':'9102','Cecil':'3258'}

Creating Dictionary:
• It is a collection of an unordered set of key:value pairs which are enclosed
in curly braces. The key value pairs are seperated by commas.
Eg: Alice: 2341

• The words to the left of colon are called as keys and the words to the right
of the colon are called as values.

• The dictionary is indexed by keys. Dictionary keys are case sensitive.

• Syntax: dictionary_name ={key_1:value_1, key_2:value_2,


key_3:value_3….key_n:value_n}
Mixed Dictionary:
The keys and their associated values can be of different types.
>>>mixed_dict={“portable”:”laptop”, 9:11,7:”julius”}
>>>dict(mixed_dict)
<class ‘dict’>
Empty dictionary can be created with a pair of curly braces without any key value
pairs.
Syntax: dictionary_name={}
Slicing in dictionaries is not allowed since they are not ordered like lists.
The dictionary statements are ordered key:value pairs, ordered by “insertion
ordered” (dictionaries remember the order in which key:value pairs were
inserted.
Two dictionaries having same key:value pairs but in different order can be
compared. It results in boolean True value.
Modifying key:value pairs in Dictionaries:
Syntax for accessing the value for a key in dictionary is,
dictionary_name[key].
The syntax to modify this is
dictionary_name[key] = value
Eg:
>>>charms= {“ring”:1009, “chain”:1010, “bracelet”:3000, “socks”:4000, “dress”:2000}
>>>charms[“anklet”:5000]
>>>charms
>>>{“ring”:1009, “chain”:1010, “bracelet”:3000, “socks”:4000, “dress”:2000, “anklet”,
5000}
Dictionaries are mutable.
Membership operators can be used along with dictionary key to check if the key is
present in dictionary or not.
>>>“Shoes” in charms
False
>>>“Shoes” not in charms
True
The dict() function:
The built-in function dict() is used to create dictionary.
Syntax : dict([**kwarg])
Eg: numbers=dict(one=1,two=2,three=3)
>>>numbers
{‘one’:1,’two’:2,’three’:3}
Built-in Functions:
Colleges={“KLE SNC”: 1955, “National College”: 1945, “Vijaya College”:1950}
Built-in Description example
Functions
len() Returns the number of items (key:value >>>len(Colleges)
pairs) in dictionary 3
all() Returns true value if all the keys in the >>>dictvar={0: True, 2:False}
dictionary are True else returns false >>>all(dictvar)
False
>>>dictvar={1: True, 2:False}
>>>all(dictvar)
True
any() Returns true value if any of the keys in the >>>dictvar={0: True, 2:False}
dictionary are True else returns false >>>any(dictvar)
True
sorted() Returns a list of items, which are sorted >>>sorted(Colleges)
based on dictionary keys [‘KLE SNC’, ‘National College’,
‘Vijaya College’]
>>>sorted(Colleges, reverse = True)
[‘Vijaya College’, ‘National college’,
‘KLE SNC’]
Methods in Dictionary:
>>>dir(dict)
Method to create a dictionary:
fromkeys() : Syntax: dictionary_name.fromkeys(seq [, value])
Currency={“India”:”Rupee”, “USA”:”Dollar”, “Russia”:”Ruble”, “Japan”:”Yen”,
“Germany”:”Euro”}

get() : syntax: dictionary_name.get(key [,default])


It returns the value associated with the key. If the key is not found, then it
returns the specified key.

Values(): syntax: dictionary_name.values()


It returns the view consisting of all the values in the dictionary.

Keys(): syntax: dictionary_name.keys()


It returns the view consisting of all the keys in the dictionary.

Popitem(): syntax: dictionary_name.popitem


It removes and returns an arbitrary tuple pair from the dictionary. Top of the
dictionary is popped out.
Pop(): Syntax: dictionary_name.pop(key)
It removes the key from the dictionary and returns its value.

Clear(): Syntax: dictionary_name.clear()


It removes all the key value pairs from the dictionary.

Update([key]:[value]) :
This function updates the key value pairs and is automatically added to the
dictionary.

Nested dictionaries:
Syntax: dictionary_name[“dict_marks”]
The dictionairies can be defined inside other dictionary.

The del Statement: Syntax: del dictionary_name[key]


To delete the key:value pair, we use the del statement followed by the name of
the dictionary.

You might also like