0% found this document useful (0 votes)
8 views5 pages

Dictionaries

A dictionary in Python is an unordered collection of key-value pairs where keys must be unique and immutable. It allows for fast lookup of values when the key is known. Dictionaries are defined using curly braces {} and contain keys and their associated values separated by a colon. Some common dictionary methods include items() to return a list of (key, value) tuples, keys() to return list of keys, and values() to return list of values.

Uploaded by

prabhu ji
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)
8 views5 pages

Dictionaries

A dictionary in Python is an unordered collection of key-value pairs where keys must be unique and immutable. It allows for fast lookup of values when the key is known. Dictionaries are defined using curly braces {} and contain keys and their associated values separated by a colon. Some common dictionary methods include items() to return a list of (key, value) tuples, keys() to return list of keys, and values() to return list of values.

Uploaded by

prabhu ji
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/ 5

Dictionaries

Python has many data structures and one of them is dictionary, an unordered, indexed
collection of items where items are stored in form of key value pairs. And keys are unique
whereas values can be changed, dictionaries are optimized for faster retrieval when the key is
known, values can be of any type but keys are immutable and can be only string, numbers or
tuples and dictionaries are written inside curly braces.

Syntax: { Key : Value }

Example:

{ ‘A’:’Apple’ ,’B’:’Ball’,’c’:’cat’,’d’:’dog’,’e’:’elephant’, ‘f ‘:’frog’,’g’:’gel’,’h’:’head’,’i’:’impala’,’j’:’jug’ }

As mentioned above the dictionary is wrapped within curly braces with a key, value format
associated with it. in our above example, ‘ A ‘ acts as the key and ‘Apple’ is the value associated
with it. In the python dictionary, the concept of the primary key is strictly maintained. that
means like additional than just the once the equivalent key cannot be assigned.

Dictionary functions

d = {1:"APPLE", 2:"ORANGE", 3:"BANANA", 4:"GUAVA", 5:"PEAR", 6:"PEACH", 7:"KIWI"}

print(d)
print(d.items())
print(d.keys())
print(d.values())

print(d.get(1))
print(d.get(11))
print(d.get(1,"Does not exist"))
print(d.get(11,"Does not exist"))

d1 = d.fromkeys([2,3,7])
print(d1)
d1 = d.fromkeys([1,2,3],"Apple")
print(d1)
d1 = d.fromkeys((1,2,3),("Apple","Orange","Banana"))
print(d1)
d.setdefault(8,"PINEAPPLE")
print(d)
d.setdefault(7,"PINEAPPLE")
print(d)
d.setdefault(9)
print(d)

d.update({10:"MANGO", 9:"CHERRY"})
print(d)

print(d.popitem())
print(d)

print(d.pop(9))
print(d)
print(d.pop(9,"Does no exist"))
print(d)
print(d.pop(100)) #gives error due to incorrect index
print(d)
print(d.pop(100,"Does no exist"))
print(d)

d.clear()
print(d)
del d
print(d) #Error as dictionary no longer exists

x = sorted(d)
print(x)
x = sorted(d, reverse=True)
print(x)
x = sorted(d.keys())
print(x)
#sorted(d) and sorted(d.keys()) works on keys and return the same result

x = sorted(d.values())
print(x)
x = sorted(d.items())
print(x)
#All sorted() functions return a list of either keys, values or (key, value) tuples
#Keys must be homogeneous (of same type) for the working of sorted(), min(), max()
and must be of number type for sum()

print(min(d))
print(max(d))
print(sum(d))
# min(), max() and sum() work on keys only

d2 = d.copy()
d2[4]="MANGO" # does not change original dictionary if value of key is immutable
print(d)
print(d2)

d2 = d
d2[4]="MANGO" # changes original dictionary
print(d)
print(d2)
# d and d2 are aliases of the same dictionary
# no new dictionary created, both d and d2 refer to the same dictionary
# Both = and copy () create shallow copies, while deepcopy() creates deep copies. For
using deepcopy(), a module copy() is imported.

'''
'''
'''
d.clear()
print(d)

del d
print(d) #Error as dictionary no longer exists

print(d)
print(d.items())
print(d.keys())
print(d.values())

print(d.get(1))
print(d.get(11))
print(d.get(1,"Does not exist"))
print(d.get(11,"Does not exist"))

d1 = d.fromkeys([2,3,7])
print(d1)
d1 = d.fromkeys([1,2,3],"Apple")
print(d1)
d1 = d.fromkeys((1,2,3),("Apple","Orange","Banana"))
print(d1)

d.setdefault(8,"PINEAPPLE")
print(d)
d.setdefault(7,"PINEAPPLE")
print(d)
d.setdefault(9)
print(d)

d.update({10:"MANGO", 9:"CHERRY"})
print(d)

print(d.popitem())
print(d)
print(d.pop(6))
print(d)
print(d.pop(9,"Does no exist"))
print(d)
print(d.pop(100)) #gives error due to incorrect index
print(d)
print(d.pop(100,"Does no exist"))
print(d)
'''

You might also like