0% found this document useful (0 votes)
12 views14 pages

Dictionary PDF

Uploaded by

sunaina bokolia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views14 pages

Dictionary PDF

Uploaded by

sunaina bokolia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Dictionary

• Python's dictionaries are kind of hash table type.


They work like associative arrays and consist of key-
value pairs.
• Dictionaries are enclosed by curly braces ({ }) and
values can be assigned and accessed using square
braces ([])
Important features of dictionaries are:
• 1. Each key of Dictionary is mapped to a value.
• 2. Each key is seperated from its value by a colon(:)
• 3. Entire dictionary is enclosed in curly braces.
• 4. Keys are unique while value s may not.
• 5.The values of dictionary can be of any type(Mutable
or immutable) but the keys must be of immutable
type(String, numbers, Tuple)
• 6. Dictionary is mutable. We can add new items or
change the value in existing item.

Commonly used dict methods:
1. <Dictionary Name>={‘key’:’Value’,’Key’:’Value’……}

• 2.Empty Dictionary
• D={ }
• 3. The function dict() is used to create a dictionary with no items
• D=dict()

• keys() - returns an iterable of all keys in the dictionary in the form of list.
• values() - returns an iterable of all values in the dictionary in the form of list.
• items() - returns an iterable list of (key, value) tuples i.e in the form of list of
tuples..
Example of Dictionary
dict = {'name': ‘Ram','code':1234, 'dept': ‘KVS'}
print(dict) # Prints complete dictionary
print(dict.keys()) # Prints all the keys
print(dict.values()) # Prints all the values
print(dict.items())
Output:
{'dept': 'KVS', 'code': 1234, 'name': 'Ram'}
['dept', 'code', 'name']
['KVS', 1234, 'Ram']
[('dept', 'KVS'), ('code', 1234), ('name', 'Ram')]
there is a second way to declare a dict:
sound = dict(dog='bark', cat='meow', snake='hiss')
print(sound.keys()) # Prints all the keys
print(sound.values()) # Prints all the values
Output:
['cat', 'dog', 'snake']
['meow', 'bark', 'hiss']
Appending values in dictionary

We can add new elements to the existing


dictionary , extend it will single pair of values or
join two dictionaries into one
Updating Elements in a Dictionary

If a key is there in the dictionary then it


If a key is there in the dictionary then
will update the value of that particular
it will update the value of that
key in the dictionary
particular key in the dictionary
Updating Elements in a Dictionary
Updating Elements in a Dictionary
Removing an element from a Dictionary
We can remove item from the existing dictionary by using del command or using pop()
method:
Using del command:

If a key is not there in the dictionary


then it will give KeyError
Removing an element from a Dictionary
Using pop() method : If a key is not mentioned then it will
give TypeError
Removing an element from a Dictionary
Commonly Dictionary Methods & Functions :
A few things we already saw on list
work the same for dict:
Similarly to how we can index into lists we use d[key] to access
specific elements in the dict. There are also a number of methods
available for manipulating & using data from dict.
• len(d) gets the number of item in the dictionary. print
(len(dict))
• key in d checks if k is a key in the dictionary. print ('name' in
dict) (True/False)
• d.pop(key) pops an item out of the dictionary and returns it,
similarly to how list’s pop method worked. dict.pop('name')

You might also like