Python-Old-Version/C3/Data-Structures-Dictionaries-and-Sets/English

From Script | Spoken-Tutorial
Jump to: navigation, search

Welcome friends.

In previous tutorial we covered Lists, Tuples and related functions. In this session we shall continue with Python data structures and cover Dictionaries and sets. We have already covered some basics of Dictionaries in session on Statistics. Here we shall revisit those concepts and some new ones.

We give it a name and it returns a corresponding number. Dictionaries are just key-value pair. For each 'key' there is corresponding 'value' associated with it. In lists we use indexes to access elements, here we use the 'key'.

Lets start by opening IPython interpreter. '{}' are used to create Python dictionaries. Lets create a dictionary say

player = {'Mat': 134,'Inn': 233,
          'Runs': 10823, 'Avg': 52.53}

Let's see what player contains by typing:

print player

Its a dictionary storing statistics of a cricket player. Here 'Mat', 'Inn' etc are the keys. Now in order to get the 'average' of this player we simply type

print player['Avg']
52.53

To add a new key-value pair to this dictionary we type

player['Name'] = 'Rahul Dravid'
print player

As you can see the given key-value pair has been added. Please note that Python dictionaries don't maintain the order in which the key-value pairs are stored. The order might change as we add new entries.

In dictionaries Duplicate keys are overwritten, that is when we do

player['Mat'] = 139

It won't create a new entry, rather it will simply overwrite previous value with the new one. So

print player

will have updated value

As we covered in one of previous sessions 'for' can be used to iterate through lists. The same is possible in case of dictionaries too. We can iterate over them using the 'keys', for example:

for key in player:
    print key, player[key]

This prints the keys in the dictionary along with their corresponding values. Notice that the order is not the same as we entered it.

We saw how containership works in lists. There we can check if a value is present in a list or not but in case of Dictionaries we can only check for the containership of the keys. so

'Inn' in player

returns True

'Econ' in Player

returns False as there is no such 'key' If you try to look or search for a 'value' it will not work.

Dictionaries support functions to retrieve keys and values such as

player.keys()

returns the list of all 'keys'

player.values()

return list of all 'values'

Now we shall move on to 'sets'. Sets in Python are an unordered collection of unique elements. This data structure comes in handy in situations while removing duplicates from a sequence, and computing standard math operations on sets such as intersection, union, difference, and symmetric difference.

Lets start by creating a set

f10 = set([1,2,3,5,8])

And thats how a set is created.f10 is the set of Fibonacci numbers less than 10 lets print the value of f10

print f10

As we mentioned earlier, these are unordered structure so order of elements is not maintained, and output order is different than input order, just as in dictionaries. Lets create one more set, a set of all prime numbers less than 10

p10 = set([2,3,5,7])
print p10.
   

To get union of these two sets we use the or '|' operator

f10 | p10

For intersection we use the and '&' operator:

f10 & p10
   
f10 - p10 

gives difference between f10 and p10, that is, the set of all elements present in f10 but not in p10. The carat '^' operator gives us the symmetric difference of 2 sets. That is f10 union p10 minus f10 intersection p10.

f10 ^ p10

To check if a set is the super set or a subset of another set, the greater than and the lesser than operators are used

set([2,3]) < p10

returns True as p10 is superset of given set

Similar to lists and dictionaries, sets also supports containership so

2 in p10

returns True as 2 is part of set p10 and

4 in p10

returns False.

The 'len' function works with sets also:

len(f10) 

returns the length, which is 5 in this case. We can also use 'for' loops to iterate through a set just as with dictionaries aists.

With this we come to the end of this tutorial on Dictionaries and sets. We have seen how to initialize dictionaries, how to index them using keys and a few functions supported by dictionaries. We then saw how to initialize sets, perform various set operations and a few functions supported by sets. Hope you have enjoyed it, Thank you.

Contributors and Content Editors

Pravin1389