L5 Dicitionary & Sets

L5 Dicitionary & Sets

DICITIONARY

Dicitionary is used to store data in key value pair format

The dictionary is the data type in Python, which can simulate the real-life data arrangement where some specific value exists for some particular key. It is the mutable data-structure. The dictionary is defined into element Keys and values.

  • Keys must be a single element , keys should be immutable python object. i.e. number strings or tuple

**Dicitionary is ordered and mutable and it cannot contain duplicate KEYS . **

  • Value can be any python object

**SYNTAX - ** name_of_variable = {''key'' : ''value'' , ''key2'' : ''value2''}

As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered.

When we say that dictionaries are ordered, it means that the items have a defined order, and that order will not change.

Unordered means that the items does not have a defined order, you cannot refer to an item by using an index.

Accessing the values of dicitionary in python.

The values of list tuple and strings can be accessed in python using indexing.

However, the values can be accessed in the dictionary by using the keys as keys are unique in the dictionary.

The dictionary values can be accessed in the following way.

1.png

1.1.png

1.2.png

Changing the values of dicitionary in python.

2.png

2.1.png

2.2.png

DICITIONARY METHODS

name_of_dicitionary.items()

name_of_variables.items() returns a list of tuples containing the key-value pairs in the given variable.

3.png

name_of_dicitionary.keys()

It returns a list of keys of the given dicitionary.

4.png

4.1.png

name_of_dicitionary.values()

It returns a list of all the values in the dictionary.

5.png

name_of_dicitionary.update()

Updates the dicitionary with elements from another dicitionary or with the supplied key-value pairs.

6.png

name_of_dicitionary.update() method will change the original dicitionary , it will not return a new value.

name_of_dicitionary.get(key)

It will return the value of the given key . If the key is not present in the dicitionary then by default it will return 'none'.

we can access the values of a given key of the dicitionary by .get() method as well as by print(name_of_dicitionary[key]) method , but if we'll try to access a key that is not given in dicitionary then .get() method will by default return 'none' but print(name_of_dicitionary[key]) method will throw an error.

7.png

SETS

Sets are used to store multiple items in a single variable.

Set is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Tuple, and Dictionary, all with different qualities and usage.

**SYNTAX - ** name_of_set{'gaurav' , 99 , True}

**Set items are unordered , Unordered means that the items in a set do not have a defined order. Set items can appear in a different order every time you use them, and cannot be referred to by index or key. Set cannot contain duplicate values , if you will create a set with duplicate values then the duplicate value will get ignored.

We cannot change items of a set . However we can delete and add items in a set. **

8.png

How to create an empty set

9.png

Set Methods

name_of_set.add()

This method adds the given element to the given set. We cannot add dicitionary or list to a set because their value can be changed , but we can add tuple to a set because it's value cannot be changed.

10.png

len(name_of_set)

This method returns the length of the given set.

11.png

name_of_set.remove()

This method updates and removes the specified element from the given set . If the given element is not present in the set then it will return an error.

12.png

name_of_set.pop()

This method removes an arbitrary item from the set and returns the removed item.

13.png

name_of_set.clear()

This method empties the given set.

14.png

name_of_set.union(a,b,c)

It does union operation on the given set with the sets a , b and c.

error.png

In python True = 1 and False = 0 that's why in the above union operation both 1 and True are not displayed in the variable z .

name_of_set.intersection(a,b,c)

It does intersection operation on the given set with the sets a , b and c.

15.png