0% found this document useful (0 votes)
3 views13 pages

Python Dictionary: Creation & Usage Guide

Uploaded by

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

Python Dictionary: Creation & Usage Guide

Uploaded by

kanakshukla290
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Dictionary in

Python
Dictionary in Python
• Dictionaries in Python is a data structure, used to store values in
key: value format. This makes it different from lists, tuples, and
arrays as in a dictionary each key has an associated value.
• Syntax
dict_var = {key1 : value1, key2 : value2, …..}
How to Create a Dictionary
• In Python, a dictionary can be created by placing a sequence of elements within curly
{} braces, separated by a ‘comma’. The dictionary holds pairs of values, one being the
Key and the other corresponding pair element being its Key:value. Values in a
dictionary can be of any data type and can be duplicated, whereas keys can’t be
repeated and must be immutable.
• Dictionary keys are case sensitive, the same name but different cases of Key will be
treated distinctly.
• The code demonstrates creating dictionaries with different types of keys. The first
dictionary uses integer keys, and the second dictionary uses a mix of string and
integer keys with corresponding values. This showcases the flexibility of Python
dictionaries in handling various data types as keys.
Dict = {1: 'My', 2: 'Python', 3: 'Goals'}
print("\nDictionary with the use of Integer Keys: ")
print(Dict)
Dict = {'Name': 'George', 1: [1, 2, 3, 4], 3.14:'PI'}
print("\nDictionary with the use of Mixed Keys: ")
print(Dict)
Different Ways to Create a Python Dictionary
• A dictionary can also be created by the built-in function dict(). An
empty dictionary can be created by just placing curly braces{}.

• The code demonstrates different ways to create dictionaries in


Python. It first creates an empty dictionary, and then shows how
to create dictionaries using the dict() constructor with key-value
pairs specified within curly braces and as a list of tuples.
Dict = {}
print("Empty Dictionary: ")
print(Dict)
Dict = dict({1: 'One', 2: 'Two', 3: 'Three'})
print("\nDictionary with the use of dict(): ")
print(Dict)
Dict = dict([(1, 'One'), (2, 'Two')])
print("\nDictionary with each item as a pair: ")
print(Dict)
Nested Dictionaries
• Example: The code defines a nested dictionary named ‘Dict’ with multiple levels of
key-value pairs. It includes a top-level dictionary with keys 1, 2, and 3. The value
associated with key 3 is another dictionary with keys ‘A,’ ‘B,’ and ‘C.’ This showcases
how Python dictionaries can be nested to create hierarchical data structures.

Dict = {1: 'One',


2: 'Tw0',
3: {'A': 'Ant', 'B': 'Bat', 'C': 'Cat'}}
print(Dict)
Adding Elements to a Dictionary
• The addition of elements can be done in multiple ways. One
value at a time can be added to a Dictionary by defining value
along with the key e.g. Dict[Key] = ‘Value’.

• Updating an existing value in a Dictionary can be done by using


the built-in update() method. Nested key values can also be
added to an existing Dictionary.

• While adding a value, if the key-value already exists, the value


gets updated otherwise a new Key with the value is added to the
Dictionary.
Add Items to a Python Dictionary with Different DataTypes
Dict = {}
print("Empty Dictionary: ")
print(Dict)
Dict[0] = 'zero'
Dict[2] = 'two'
Dict[1] = 'one'
print("\nDictionary after adding 3 elements: ")
print(Dict)
Dict['any'] = 2, 3, 4
print("\nDictionary after adding 1 more element: ")
print(Dict)
Dict[2] = 'two two'
print("\nUpdated key value: ")
print(Dict)
Dict['every'] = {'Nested': {'A': 'Ant', 'B': 'Bat'}}
print("\nAdding a Nested Key: ")
print(Dict)
Accessing Elements of a Dictionary
• To access the items of a dictionary refer to its key name. Key
can be used inside square brackets.
Dict = {1: 'One', 'two': 2, 3: 'Three'}
print("Accessing a element using key:")
print(Dict['two'])
print("Accessing a element using key:")
print(Dict[3])
Deleting Elements using ‘del’ Keyword
• The items of the dictionary can be deleted by using the del keyword as given below.
Dict = {1: 'One', 'two': 2, 3: 'Three'}
print("Dictionary =")
print(Dict)
del(Dict[1])
print("Data after deletion Dictionary=")
print(Dict)
del(Dict[‘two’])
print(Dict)
Thankyou
Made by Sukanya Rakshit

You might also like