Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

Dictionary in Python

Python dictionaries are mutable objects that store key-value pairs. They can be created using curly braces {} or the dict() function. Keys must be immutable but values can be any data type. Values can be added, updated, or removed using dictionary methods like update(), pop(), clear(), etc. or by accessing the key and assigning a new value. Dictionaries provide fast access and retrieval of values using keys in Python.

Uploaded by

vivekbc71
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Dictionary in Python

Python dictionaries are mutable objects that store key-value pairs. They can be created using curly braces {} or the dict() function. Keys must be immutable but values can be any data type. Values can be added, updated, or removed using dictionary methods like update(), pop(), clear(), etc. or by accessing the key and assigning a new value. Dictionaries provide fast access and retrieval of values using keys in Python.

Uploaded by

vivekbc71
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Dictionary in Python

Omputer
Science -
XII
Santosh Kumar
PGT Comp. Sc.

Jawahar Navodaya Vidyalaya


Python Data Types
Data types are the classification or categorization of data items. It represents the
kind of value that tells what operations can be performed on a particular data. Since
everything is an object in Python programming, data types are actually classes and
variables are instance (object) of these classes. Following are the standard or
built-in data type of Python:
Data Type in
Python

Numeric Dictionary Set Sequence


Boolean
Type
Number Strings
Immutable mutable
Float List
Complex Tuple
Dictionary
Python dictionary is unordered collection of elements. The difference between Python
dictionary and other uordered Python data types such as sets lies in the fact that unlike
sets, a dictionary contains keys and values rather than just elements. Like lists, Python
dictionaries can also be changed and modified. Each key-value pair in a Dictionary is
separated by a colon :, whereas each key is separated by a ‘comma’.
Creating a dictionary
In Python, a Dictionary can be created by placing a sequence of elements within
curly {} braces, separated by ‘comma’. Dictionary holds a pair of values, one being the
Key and the other corresponding pair element being its Key:value. Values in a dictionary
can be of any datatype and can be duplicated, whereas keys can’t be repeated and must be
immutable.
Dictionary can also be created by the built-in function dict(). An empty dictionary can
be created by just placing to curly braces{}.
Note – Dictionary keys are case sensitive, same name but different cases of Key will be
treated distinctly.
# Creating an empty Dictionary
Dict = {} Output
print("Empty Dictionary: ") Empty Dictionary:
print(Dict) {}
# Creating a Dictionary with Integer Keys Dict = {1:
‘Data', 2: 'For', 3: ‘Data'} print("\nDictionary with
the use of Integer Keys: ") print(Dict) Dictionary with the use of Integer Keys:
# Creating a Dictionary with Mixed keys Dict = {1: 'Geeks', 2: 'For', 3: 'Geeks’}
{'Name': 'Geeks', 1: [1, 2, 3, 4]}
print("\nDictionary with the use of Mixed Keys: ")
print(Dict) Dictionary with the use of Mixed Keys:
# Creating a Dictionary with dict() method {1: [1, 2, 3, 4], 'Name': 'Geeks’}
Dict = dict({1: ‘Data', 2: 'For', 3:’Data'})
print("\nDictionary with the use of dict(): ")
print(Dict) Dictionary with the use of dict():
# Creating a Dictionary with each item as a Pair {1: ‘Data', 2: 'For', 3: ‘Data’}
Dict = dict([(1, ‘Data'), (2, 'For')])
print("\nDictionary with each item as a pair: ")
print(Dict) Dictionary with each item as a pair:
{1: ‘Data', 2: 'For'}
Adding elements to a Dictionary
In Python Dictionary, 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.
Note – 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.
# Creating an empty Dictionary
Dict = {} Output:
print("Empty Dictionary: ") Empty Dictionary:
print(Dict) {}
# Adding elements one at a time
Dict[0] = ‘Data'
Dict[2] = ‘Jawahar'
Dict[3] = 1
print("\nDictionary after adding 3 elements: ") Dictionary after adding 3 elements:
print(Dict) {0: 'Data', 2: 'For', 3: 1}
# Updating existing Key's Value
Dict[2] = 'Welcome'
print("\nUpdated key value: ") Updated key value:
print(Dict) {0: 'Data', 2: 'Welcome', 3: 1}
Removing Elements from Dictionary
In Python Dictionary, deletion of keys can be done by using the del keyword. Using del keyword,
specific values from a dictionary as well as whole dictionary can be deleted. Other functions
like pop() and popitem() can also be used for deleting specific values and arbitrary values
from a Dictionary. All the items from a dictionary can be deleted at once by
using clear() method.
# Initial Dictionary
Dict = { 5 : 'Welcome', 6 : 'To', 7 : 'Geeks',
'A' : {1 : 'Geeks', 2 : 'For', 3 : 'Geeks'},
'B' : {1 : 'Geeks', 2 : 'Life'}}
print("Initial Dictionary: ")
print(Dict)
# Deleting a Key value
del Dict[6]
print("\nDeleting a specific key: ")
print(Dict)
# Deleting a Key using pop()
Dict.pop(5)
print("\nPopping specific element: ")
print(Dict)
# Deleting an arbitrary Key-value pair # using popitem()
Dict.popitem()
print("\nPops an arbitrary key-value pair: ")
print(Dict)
# Deleting entire Dictionary
Dict.clear()
print("\nDeleting Entire Dictionary: ")
print(Dict)

Output:
Initial Dictionary: {'B': {1: 'Geeks', 2: 'Life'}, 'A': {1: 'Geeks', 2: 'For', 3:
'Geeks'}, 5: 'Welcome', 6: 'To', 7: 'Geeks’}
Deleting a specific key: {'B': {1: 'Geeks', 2: 'Life'}, 'A': {1: 'Geeks', 2: 'For', 3:
'Geeks'}, 5: 'Welcome', 7: 'Geeks’}
Popping specific element: {'B': {1: 'Geeks', 2: 'Life'}, 'A': {1: 'Geeks', 2: 'For', 3:
'Geeks'}, 7: 'Geeks’}
Pops an arbitrary key-value pair: {'A': {1: 'Geeks', 2: 'For', 3: 'Geeks'}, 7: 'Geeks’}

Deleting Entire Dictionary: {}

You might also like