Learn Python like a Professional! Take You from 0 to Hero
In this Python lecture, we will learn about the Dictionary.
"Dictionary is an unordered collection of items i.e key-value pairs ".
Let's look at Dictionary example-
Create Dictionary
Dictionary is defined by placing item i.e key-value pair, inside curly braces {} separated by a comma. Each key is separated from its value by a colon (:).
my_dictionary = { 'key1': 1 ,'key2': 2 ,'key3': 3 } # here key Key1, key2 & key3 is mapped with the value 1, 2 & 3 repectively
print(my_dictionary)
Access Dictionary Values
We can use the key inside the index operator [] to access a value in a dictionary.
my_dictionary = { 'key1': 1 ,'key2': 2 ,'key3': 3 }
print( my_dictionary['key1'] ) # garbs the value mapped with key1
We have an option in python that list can hold different object type (integer, float, string etc).
my_dictionary={ 'key1': 1, 'key2': 1.5,'key3': 'string'}
print( my_dictionary )
my_dictionary={ 'key1': 1, 'key2': 1.5,'key3': 'string'}
print( my_dictionary['key3'] )
Since the value corresponding to key3 is a string object, so we can perform all thing we did in string lecture.
print( my_dictionary['key3'][0] ) # grab the letter at index 0
print( my_dictionary['key3'][-1] ) # reverse the string
print( my_dictionary['key3'].upper( ) ) # Convert the string to uppercase
Mutability
Dictionary is mutable, the values within it can be changed or replaced, unlike string or tuple.
We can use the assignment operator (=) to change an item or a range of items.
my_dictionary={ 'key1': 1, 'key2': 1.5,'key3': 'string'}
my_dictionary['key1'] = my_dictionary['key1'] + 99
print( my_dictionary['key1'] )
print( my_dictonary )
We can also use subtraction to change the 'key1' value
my_dictionary['key1'] - = 50
print( my_dictionary['key1'] )
print( my_dictionary )
Another Method of Creating Dictionary
We can also create a dictionary with an empty dictionary
# Create a empty dictionary
d = {}
print( d )
>> { }
# Insert the key-value pairs in the empty dictionary
d['student' ] = 'Tamta'
d['roll_no'] = 40
print( d )
>> { 'student' : 'Tamta' , 'roll_no' : 40 }
We can also create a dictionary with an empty dictionary
# Create a empty dictionary
d = {}
print( d )
# Insert the key-value pairs in the empty dictionary
d['student' ] = 'Tamta'
d['roll_no'] = 40
print( d )
Nested dictionary
Dictionary could have another dictionary as a key-value pair. This is called a nested dictionary.
d = {'key' : {'nestedkey' : {'subnestedkey' : 'value' }}}
print( d )
>> {'key': {'nestedkey': {'subnestedkey': 'value'}}}
We can use keys to extract the value in the nested list.
Let's see how to grab the value.
# keep calling the keys
print( d['key']['nestedkey']['subnestedkey'] )
>> 'value'
Since the value is a string object, so we can perform all thing we did in string lecture.
print( d['key']['nestedkey']['subnestedkey'].upper( ) )
>> VALUE
d = {'key' : {'nestedkey' : {'subnestedkey' : 'value' }}}
print( d )
print( d )
>> {'key': {'nestedkey': {'subnestedkey': 'value'}}}
We can use keys to extract the value in the nested list.
Let's see how to grab the value.
# keep calling the keys
print( d['key']['nestedkey']['subnestedkey'] )
>> 'value'
Since the value is a string object, so we can perform all thing we did in string lecture.
print( d['key']['nestedkey']['subnestedkey'].upper( ) )
>> VALUE
Built-in Function
Methods that are available with Dictionary.
d = { 'k1' : 1, 'k2': 2, 'k3': 3}
print( d )
>> { 'k1' : 1, 'k2': 2, 'k3': 3 }
print( d.keys() ) # Return a new view of the dictionary's keys.
print( d.values() ) # Return a new view of the dictionary's values.
print( d.items() ) # Return a new view of the dictionary's key-value pair.
d.pop( 'k1' ) # remove specified key and return the corresponding value
print( d )
d.clear() # remove all the items from the dictionary
print(d)
Practice other built-in functions available in the list.
In the next Blog, we will discuss Tuple
https://sngurukuls247.blogspot.com/2018/09/python-ninja-bootcamp-9-tuple.html
......................................................................................................................................
Instagram-
https://www.instagram.com/python.india/
View the Jupyter Notebook for this lecture
Download the Jupyter Notebook for this lecture
https://sngurukuls247.blogspot.com/2018/09/python-ninja-bootcamp-9-tuple.html
......................................................................................................................................
Follow the link below to access Free Python Lectures-
https://www.youtube.com/channel/UCENc9qI7_r8KMf6-_1R1xnwInstagram-
https://www.instagram.com/python.india/
View the Jupyter Notebook for this lecture
Feel free contact me on-
Email - sn.gurukul24.7uk@gmail.com
No comments:
Post a Comment