Python Dictionary is another compound type like list and tuple in Python. It is different from these two compound types because you can use immutable keys to access elements instead of index. So, let’s understand how to create dictionaries and performs operations on it.
Creating Python Dictionary
Python dictionary is created in two ways. One, by creating an empty Python dictionary with empty curly parenthesis and add elements using the keys. Second method is by declaring keys and elements pairs enclosed in curly parenthesis. Keys can be integer or string.
Method 1
dictionary_name={}
dictionary_name[‘key1’]=value1
dictionary_name[‘key2’]=value2…….
In this declaration the keys are always strings. Values can be of any data type. If string enclose values in single quotes
Example
Weekdays={} Weekdays[1]=’Monday’ Weekdays[2]=’Tuesday’ Weekdays[3]=’Wednesday’ Weekdays[4]=’Thursday’ Weekdays[5]=’Friday’ Weekdays[6]=’Saturday’ Weekdays[7]=’Sunday’
Method 2
dictionary_name={‘key1’:’value1’,’key2’:’value2’,…..}
Weekdays={ ‘mon’:’Monday’, ‘tue’:’Tuesday’, ‘wed’:’Wednesday’, ‘thu’:’Thursday’, ‘fri’:’Friday’, ‘sat’:’Saturday’, ‘sun’:’Sunday’}
Accessing elements in Python Dictionary
Elements in a Python Dictionary are accessed just like accessing array elements- using square parenthesis after the name of the dictionary. But with dictionary you have to give the key of the element you wish to access.
Dictionary_name[‘key’]
The element accessed in this way can be printed, assigned to a variable or used for processing.
weekday[‘wed’] returns Wednesday
Deleting elements from Python Dictionary
Since elements can be accessed in a dictionary using keys so any element can be deleted by using the key. Python function del is used for deleting any element from the dictionary. The syntax is
del Dictionary_name[‘key’]
Once element is deleted it is no longer part of the dictionary.
Example
del weekday[‘wed’] #removes Wednesday from the weekday dictionary
Counting elements in a Python Dictionary
Counting elements is essential if some iterative processing is done on the dictionary elements. Python function len is used for getting the count of elements in a dictionary. The syntax is
len( Dictionary_name)
Example
len( weekday) #returns 7
Methods used with Dictionary
When a dictionary is declared and created, certain inbuilt methods can be called for getting information about a dictionary.
keys()
When this method is called for a declared dictionary, it returns a list of keys associated with it. It does not accept any arguments. The function is called like this
dictionary_name.keys()
weekday.keys()
values()
When this method is called for a declared dictionary, it returns a list of values stored in the dictionary. It does not accept any arguments. The function is called like this
dictionary_name.values()
weekday.values()
items()
When this method is called for a declared dictionary, it returns a list of keys and the associated values stored in the dictionary. It does not accept any arguments. The function is called like this
dictionary_name. items ()
weekday. items ()
Example using Dictionary Functions and methods
# tuple Weekdays created with method 1
Weekdays={}
Weekdays[1]='Monday'
Weekdays[2]='Tuesday'
Weekdays[3]='Wednesday'
Weekdays[4]='Thursday'
Weekdays[5]='Friday'
Weekdays[6]='Saturday'
Weekdays[7]='Sunday'
# tuple planets created with method 2
planets={'me':'Mercury','ve':'venus','ea':'Earth','ma':'Mars','ju':'Jupiter','sa':'Saturn','ur':'Uranus','ne':'Neptune','pl':'Pluto'}
# Accessing an element with key
print(Weekdays[4])
# Displaying number of elements
print(len(planets))
# deleting an element
del planets['pl']
print(len(planets))
# updating an element's value
Weekdays[3]=""
print(Weekdays)
#Printing all keys
print(Weekdays.keys())
#Printing all values
print(Weekdays.values())
#Printing all key value pairs
print(Weekdays.items())
#Checking presence of a key in dictionary using in operator
print(3 in Weekdays)
Be First to Comment