Collection Data Types in Python: Part 1

Ozan Güner
Oct 23, 2020 · 5 min read

Python has a lot of different datatypes. I will mention the first two “collection data types” as first part in this post, which will be continued next week with second part; set and tuple.

  • Set
  • Tuple
  • List
  • Dictionary

I will try to explain with a simple example to be catchy. I will be using Jupyter Notebook while doing this. The Jupyter Notebook is a powerful and interactive web tool known as also a free and open-source for coding.

Let’s begin. Here is the scenario:)

Cited from:

I should go to market for shopping. Because of this I want to create a shopping list to remember what I need by using 4 different python collection data types. The things I want to buy are the following.

  • Apple — > 3 pieces
  • Milk — > 1 bottle
  • Cheese — > 250 gram
  • Orange — > 5 pieces

1. Set

I defined a set called “shoppingSet” by using curly brackets. I wrote “apple” three times. But be careful, it appears only once in the output. Because set holds unique data and it doesn’t allow duplicates. I saw that by using “len ( )” method only 4 items were in my shoppingSet.

Sets are unindexed and unordered. So I couldn’t use indexing or slicing to access set’s items. It raised TypeError. Sets are unchangeable. I can’t change their items. But I can add a new item by using “add( )” method.

If I would like to add an item like “milk” that already exist in set, I couldn’t add. Let’s remember again that set holds unique item.

I could add a new item by using “update ( )” method too.

When I would like to remove an item from set, I could use “remove ( )” method. But if I would like to remove an item that does not already exist, it raise KeyError.

I could also use “discard ( )” method to remove an item from set. It doesn’t raise an error when an item does not already exist that I would like to remove.

I could use “pop ( )” method to remove the last item of set, but I don’t prefer. Remember that sets are unordered. So we can’t know which item is removed from set when every time we run set. Because the order of the items in the set can change with each run.

This shoppingSet was not a list exactly as I want. Because I couldn’t assign a value the set items like “3 pieces” or “1 bottle” etc. so I would like to delete it.

If I use “clear ( )” method to delete set I couldn’t delete set, but I clear it and get an empty set.

I could use “del” keyword to delete set. If I would like to access shoppingSet after delete it, I can’t.

2. Tuple

Tuple is another collection data type. I defined a tuple called “shoppingTuple” with brackets. I could write “apple” three times. Because tuple allows duplicate members. Now I got 6 members in shoppingTuple.

If I would like to access the items of tuple I want, I could use indexing or slicing as you can see above. Tuples are indexed and ordered. But they are also unchangeable.

I can’t change the items inside as you see above. I can’t add new items or remove the items from tuple.

By using “count ( )” method I could access the number of times a specified value appears. You can access important methods like count () etc., which I mentioned in my previous post, “Most Common String Methods in Python”, from here.

I could access the position of a specified item where it was found in the tuple by using “index ( )”.

I still haven’t been able to make a list as I wanted. Because I couldn’t assign a value to the each tuple items. I could write three times “apple”, but how could I write “250 gram cheese”:) So I deleted the tuple by using “del” keyword.

I will mention last two of four collection types in python in my next post; list and dict. Thus we will be able to decide which collection type is most useful for our needs.

To be continued..:) See you in second part..


The Startup

Get smarter at building your thing. Join The Startup’s +731K followers.