Dictonaries, Sets & Tuples

Dictionaries

Dictionaries store the data in the form of key-value pairs. Dictionaries are written with curly brackets. Dictionaries are indexed with key values

{'key1':'value1', 'key2':'value2'}
In dictionaries, we retrive the data using key name whereas in lists, we retrive the data using index.

Example

mydict = {'key1':'value1', 'key2':'value2'}

mydict['key1']

In dictionaries, we can have any type of objects.
Dictionary can contain int,float,string,list and dictionaries too..

Lists are ordered collection whereas dictionaries are unordered collection of objects. So, we cannot sort the values of a dictionary.

Dictionaries are mutable.

mydict['key1'] = 'value3'

Popular Methods on Dictionary

Methods Description
mydict.keys() to get all keys of dictionary.
mydict.values() to get all values of dictionary.
mydict.items() to get all items(key:value) of dictionary.
mydict['key3']='value3' to add the element to the dictionary.
mydict.pop('key1') to remove the element from the dictionary.
mydict.popitem() to remove the last inserted elemnet from the dictionary.
del mydict['key1'] to remove the element from the dictionary.
del mydict to delete the dictionary.
mydict.clear() to clear the dictionary, to delete all the elements of the dictionary.
mydict2 = mydict.copy() to copy the dictionary.
mydict2 = dict(mydict) to copy the dictionary.
mydict = dict(key1='value1, key2='value2') to create a dictionary with keyword dict.
len(mydict) to get the length of the dictionary

Tuples

Tuples are similar to lists, the one key difference is tuples are immuatable (Once a element is inside in a tuple, it can't be changed).

Tuples use parenthesis.

mytuple = (1,2,3,4,5)
Tuple elements can be accesses through negative indecies as list.

Popular Methods on Tuples

Methods Description
mytuple[1] to get the second element of the tuple , accessing through index number
len(mytuple) to find the length of the tuple
count() Returns the number of times a specified value occurs in a tuple
index() Searches the tuple for a specified value and returns the position of where it was found

To change the elements of a tuple

x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)

print(x)
>> ("apple", "kiwi", "cherry")

Change Tuple Values

Inorder to create a tuple with single element, we need to add a comma after the item, otherwise Python will not recognize it as a tuple.

thistuple = ("apple",)
print(type(thistuple))
>> <class 'tuple'>

#NOT a tuple
thistuple = ("apple")
print(type(thistuple))
>> <class 'str'>

Sets

Set is a unordered and unindexed collection of objects. set is represented using curly braces.

myset = {"apple", "banana", "cherry"}

Set only takes unique elements.

unordered in the sense, we are not sure in which order they appear.

Sets are immutable, that we cannot change the elements of the set, once it is created. We can only add and delete the elements from the set.

Popular Methods on Set

Methods Description
myset.add('orange') to add element to the set
myset.update(['mango', 'grapes']) to add multiple elements to the set.
len(myset) to get the length of the set.
myset.remove('apple') to remove an element from the set.
myset.discard('apple') to remove an element from the set.
myset.pop() to remove the last entered item from the set.
myset.clear() to clear all the items of the set.
del myset to delete the set.
myset = set2.union(set1) union of elements, removes the duplicates.
myset.update(set1) to insert the items of set1 into myset.
myset = set(('apple', 'banana')) to create a set using set keyword.