Py05: Collect'em All


Py05: Collect'em All


As it seems to me, in Perl you have to be an expert to correctly make a nested data structure like, say, a list of hashes of instances. In Python, you have to be an idiot not to be able to do it, because you just write it down.
- Peter Norvig 

Let's start this post with a very basic and popular question which is asked almost everytime collections type is taught, suppose you are a teacher in a class of 60 students and you have to store the marks of a test of Python subject in variables and you've just completed the tutorial of datatypes and variables. So, the first instinct would be to create a variable for each student something like this:

 >>> py_test_adam = 85
 >>> py_test_arjun = 90

Well, good you're applying what you've learned so far. But one question, you've 60 students which means you've to create 60 variables and must remember the name of each variable correctly to perform any calculation. Furthermore, say you've super memory and you've created variable for each student separately and now you want to calculate average marks, then you've to do it something like this:

 >>> py_test_avg_marks = (py_test_adam + py_test_arjun + ...) / 60
 >>> print(py_test_avg_marks)

The above calculation of py_test_avg_marks is correct but there are two major problems here, first, you have to remember each of the variable names (in case you don't have super memory) and second, a fact is not considered here that the number 60 is redundant because that information is already with you in form of the number of values used.
So, what do we have here for the solution? Python collections type. Python offers multiple types of collections which help to get rid of the problem discussed above and provide many great additional features.
But what is a collection? A collection is something which holds multiple values for us under a single name. Think of it as a shopping list. In a shopping list, you write down all the items in a single list instead of creating a new list for every new item because you know there is a relation between every item i.e. each item belongs to that single shopping list.
So, what type of and how many "shopping list" does Python offers? Well, there are many but we'll discuss the three major ones.

# Note: The items in the following example are of integer type but it could be anything, even of the type which is used for the creation of the collection.

  1. list
    • a list is a type of collection.
    • a list can be created by providing values (also variables) separated by commas between square brackets, [].
    • Python's list is of the mutable type which means that once the list has been created, the elements can be added, modified, or deleted.
    • List's elements can be of any type.
    • A list created in following ways:
    •  >>> a_list = [1, 2, 3]
       >>> empty_list = []
       >>> char_list = list("Python")    # equals to ['P', 'y', 't', 'h', 'o', 'n']
       >>> a_list = [1, 2, 3, 3, 2, 1]
  2. set
    • a set is also a type of collection.
    • a set is always created by providing values (also variables) separated by commas in set function, set().
    • The set() function can take a list as a parameter.
    • What's special about the sets is that each and every value in the set is unique.
    • Set will hold only unique value even the values are repeated in the input.
    • Never create a set like we created lists as doing something like,  >>> a_set = (), will lead to the creation of another type i.e. tuple (discussed below)
    • A set created in following ways:
    •  >>> empty_set = set()
       >>> a_set = set("abracadabra"))    # equals to {'b', 'r', 'a', 'c', 'd'}
       >>> a_set = set([1, 2, 3, 3, 2, 1])
  3. tuple
    • a set is always created by providing values (also variables) separated by commas in ().
    • The tuple() function can take a list as a parameter.
    • What's special about the tuple is that once a tuple is created it cannot be modified.
    • tuple offers only two functions 'count' and 'index'.
    • tuple index function will return the index of the first item even if the item is repeated.
    • A tuple created in following ways:
    •  >>> a_tuple = (1, 2, 3)
       >>> empty_tuple = tuple()
       >>> empty_tuple = ()
       >>> tuple("abracadabra"))    # equals to ('a', 'b', 'r', 'a', 'c', 'a', 'd', 'a', 'b', 'r', 'a')
       >>> a_tuple = tuple([1, 2, 3, 3, 2, 1])
       >>> a_tuple = tuple((1, 2, 3, 3, 2, 1))
  4. dict
    • dict stands for dictionary
    • a dict is not a type of collection but a type of mapping.
    • a dictionary is one of the most useful collection types in Python and it is my one of the favorite feature of Python.
    • The dictionary provides a mapping facility which helps us provide key value relation in Python.
    • A sample dictionary looks like {1: "Python"}, here, is the key and "Python"  is the value of the key.
    • The keys of the dictionary are always unique.
    • Python's dictionary is of the mutable type which means that once the list has been created, the elements can be added, modified, or deleted.
    • Dictionary's elements can be of any type.
    • A dictionary created in following ways:
    •  >>> a_dictionary = {1: "Python", 2: "Java", 3: "Kotlin"}
       >>> empty_dict = {}
       >>> empty_dict = dict()
       >>> dict_with_constructors = dict(1="Python", 2="Java", 3="Kotlin")
       >>> dict_with_list_of_tuples = dict((1, "Python"), (2, "Java"), (3, "Kotlin")
       >>> dict_with_a_default_value = dict.fromkeys([1, 2, 3], "Python")    # Here "Python" is the default value
       >>> dict_with_two_lists = dict(zip([1, 2, 3], ['Python', 'Java', 'Kotlin']))
All the methods of creating dictionary are self-explanatory. Wait!!! But I didn't understand the last method, one with dict_with_two_lists. I know that last one is different with a new function, zip.

zip is BIF (built-in function) which takes iterables and return a zip object. Here this zip object is used to create the dictionary. What does zip do, it runs over the given parameters (which could a list, a set, etc.) and start associating the elements with other elements of other parameters. Like in this examples the association is created:
1 → 'Python'
2 → 'Java'
3 → 'Kotlin'

Accessing the Value

So, now have created several lists, tuples, and dictionary but how to retrieve back the items?
You can access the item by indexing method. Now, what it is? Nothing, just put the index of the item in the square bracket you want to access.
The index is the position of the item and it starts from 0 (zero) and ends at number one less than the length of the item.

>>> a_item_from_list = a_list[0]    # equals to 1
>>> a_item_from_tuple = a_tuple[2]    # equals to 3
>>> a_item_from_dict = dict_with_two_lists[3]    # equals to 'Kotlin'

Here, you might be thinking that dictionary item access is same as other types. But it is not, in fact, the number 3 used to access the item is the key, not the index.  So, if your dictionary looks like this
>>> d = {'Python': 1, 'Java': 2, 'Kotlin': 3}    # , then you would access the third item by
>>> third_item = d['Kotlin']    # equals to 3

It is not finished, Python has one more trick up to its sleeve. It supports negative indexing. What is negative indexing? It is indexing done from the last element instead of first and it starts with -1 instead of 0.
So, if you're asked to print the third last element of a list, you don't have to access by

>>> my_list[len(my_list) - 3]

though it would yield correct Python is always ready for help, you can access it by,
>>> my_list[-3]

That would print the third last item of the list.

So, these are some basic collection types available in Python. There is a lot more to learn about them. Until then, just think now you have a list computer_science_marks, thank God, only one variable instead of 60 but how would you access the marks of those 60 students? By index accessing? Typing command for each index 60 times? Think until we'll find about it in next post.

Try It Yourself:
Try all the types of collections, some cool exercise is coming after some more posts.


Happy Pythoning!!

Comments