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

Dictionary and Set

Uploaded by

yashkshatriya108
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)
12 views5 pages

Dictionary and Set

Uploaded by

yashkshatriya108
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

Dictionary & Set

Study Notes

Dictionary & Set


In
Data Analytics
Dictionary & Set

Dictionary
 Data type built in python language which is based on KEY:VALUE pair.
 Dictionary are indexed by keys which are indexed by keys, which can be any immutable type
 Main operations on dictionaries are storing and retrieving values from dictionaries.
 If you store using a key that is already in use, the old value associated with that key is forgotten.
 List cannot be used as key in dictionary as lists can be modified using methods like append and
extend

Fruits = {‘Apple’:10, ‘Oranges’: 20,’Banana’: 30}


Fruits – dictionary name
Apple (key name) :10 (value)
Oranges (key name) :20 (value)
Banana (key name) :30 (value)

Main keywords/methods used on dictionary


 Del – deletes an element from dictionary
 In – returns whether an element exists in
 Clear – clear all elements from the dictionary
 List – returns a list of all keys in dictionary
 Copy – returns a copy of dictionary
 Sorted
 Len – returns length of dictionary
 Get – returns value of specific key
 Values – returns a list of all values in dictionary

Examples:
 Create an Empty dictionary - A pair of {} creates an empty dictionary
>>> Fruits = {}

 Create a dictionary
>>> Fruits = {„Apple’:10, ‘Oranges’: 20,’Banana’: 30}

 ‘Del’ keyword in dictionary


>>> Fruits = {„Apple’:10, ‘Oranges’: 20,’Banana’: 30}
>>> del Fruits['Oranges']
>>> Fruits
>>> {‘Apple’:10,’Banana’:30}

 ‘In’ keyword in dictionary


>>> Fruits = {‘Apple’:10, ‘Oranges’: 20,’Banana’: 30}
Dictionary & Set
>>> ‘Apple’ in Fruits
>>> true

 ‘Not in’ keyword in dictionary


>>> Fruits = {‘Apple’:10, ‘Oranges’: 20,’Banana’: 30}
>>> ‘Grapes’ not in Fruits
>>> true

 ‘List’ keyword in dictionary


>>> Fruits = {‘Apple’:10, ‘Oranges’: 20,’Banana’: 30}
>>> list(Fruits)
>>> [‘Apple’,‘Oranges’,’Banana’]
 ‘Sorted’ keyword in dictionary
>>> Fruits = {‘Apple’:10, ‘Oranges’: 20,’Banana’: 30}
>>> sorted(Fruits)
>>> [‘Apple’,’Banana’,‘Oranges’]
 ‘len’ keyword in dictionary
>>> Fruits = {‘Apple’:10, ‘Oranges’: 20,’Banana’: 30}
>>> len(Fruits)
>>> 3
Dictionary & Set
SETS
 A set is an unordered collection with no duplicate elements.
 Set() can be used to create sets.
 Sets support mathematical operations like union, intersection, difference and symmetric
difference.
 Set is based on “hash table”.
 A set is not immutable unlike a tuple.
 Methods like len(), discard(), pop(), clear(), remove(), add() & max() also apply on sets.

>>> a = set('abracadabra')
>>> a
{'a', 'r', 'b', 'c', 'd'}

 Union

>>> a = set('abracadabra')
>>> b = set('alacazam')
>>> a | b

{'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}

 Difference

>>> a = set('abracadabra')
>>> b = set('alacazam')
>>> a - b
{'r', 'b', 'd'}

 Intersection

>>> a & b
{'a', 'c'}

 Remove method

sets = set([10,20,26,41,54])
print(sets.discard(20))

Output: {41,10,26,54}
Dictionary & Set
 Len method

Sets = set([10,20,26,41,54])
Print(Sets.len())

Output: 5

You might also like