Collection Data Types in Python: Part 2

Ozan Güner
The Startup
Published in
6 min readOct 30, 2020

--

I had mentioned the two of collection data types, called set and tuple, at my previous post “Collection Data Types in Python: Part 1”. In this post, I will mention last two of collection data types, called list and dictionary with the same scenario in Part 1.

Let’s remember the scenario;

I will try to do suitable shopping list by using list and dictionary in following. Thus I will decide which data type is more suitable for my needs at the end of this post.

  • Apple → 3 pieces
  • Milk → 1 bottle
  • Cheese → 250 gram
  • Orange → 5 pieces
Cited from: https://www.monsterinsights.com/

3. List

I could define a list, called “shoppingList” by using squared brackets. The “type( )” method returned to me that it is a list. By using “len ( )” method, I saw that my shoppingList has 6 items. Be careful that I could write “apple” three times because of lists allow duplicates.

Lists are indexed and ordered. So I could access the items of shoppingList by using indexing and slicing. I could reverse the list by using “[::-1]” as you could see in line 10.

Lists are changeable. I could assign different item like “ egg” to index 1 or I could assign different items from index 1 to index 3. Be careful how items of list could change. If you assign more than one item to one index as you see in line 15, lists collect it as one item. Thus my shoppingList items increase 1 item as you see in line 17.

By using “append( )” method, I could add an item to the end of the list.

Or using “insert( )” method, I could add an item to index which I want into the list.

I could remove an item from the list using “remove( )” method. Please examine line 22 and line 24 carefully. In line 22, I removed the item by using index, I also could remove the item by writing item’s name into “remove( )” method in line 24.

If you would like to remove more than one item you should use “for loop” above but the loops are not the subject of this post:)

I could use “sort( )” method for sorting the items alphabetically.

If I would like to reverse order I could use “reverse( )” method as you could see above.

As you see, lists are useful for so many different applications but I still couldn’t get a list how I want. Because I couldn’t assign a value to each item like 3 pieces of apple, or 250 gram of cheese etc. Therefore I cleared the list using “clear( )” method. Then we could get an empty list. But if we would like to delete whole “shoppingList”, we should use “del” keyword.

4. Dictionary

I could define a dictionary, called shoppingDict, by using curly brackets. Most important thing to know about dictionaries that they occur key-value pairs like {key:value}. Each key-value pair counts as 1 item. Because of this we could see the number of items as 4 in shoppingDict by using “len( )” method.

Dictionaries are unordered and they also do not allow duplicates. If I would like to access the value of an item, I could use indexing. It is enough to write the “key” name, to see the “value” of the item as you could see in line 4 and line 5.

Dictionaries are changeable. The value of the “cheese” key was “100 gram”, but I want “250 gram”. So I could change the value of “cheese”, if I write the key name by using indexing as you could see in line 6. Then I could equalize it to “250 gram”.

If I would like to see the “key” members of shoppingDict, I could use “keys( )” method. If I would like to access the “value” members of shoppingDict, I could use “values( )” method.

By using “items( )” method, I also could access whole members of shoppingDict.

I would like to check if key member I am looking for is in my shoppingDict or not. I could check it as you could see in line 11. Thus it returns “boolean” like “True” or “False”. But the most important thing is not to forget that Python is case-sensitive as I mentioned on my “Most Common String Methods in Python” post. You could only check the key members of dictionary if it is in your dictionary or not. In other words, if the member you are looking for is in the key members, it returns “True”, if not it returns “False”.

If I would like to add a new item, I could use two techniques. One of them is indexing as you could see in line 13. I wrote the key name of item I would like to add and then I equalized to a value. The other technique for adding new item is to use “update( )” method as you could see in line 15.

I could write the key name of item that I would like to remove from dictionary into “pop( )” method. Thus I could remove it from dictionary.

Finally, I reached a shopping list I am looking for with shoppingDict. But I also would like to share with you how to clear or delete a dictionary at the below as you may already know:)

I cleared the dictionary using “clear( )” method as I did it in “list” type. Thus I got an empty dictionary. But if I would like to delete whole dictionary, I should use “del” keyword as you may already know from “list” type.

Conclusion

I reached the values of items with shoppingDict. So dictionary type was suitable for my example. As you could see, I tried to explain 4 collection data types(set, tuple, list, dictionary) and the differences between them on a simple shopping example. But do not forget; each of these collection data types have different advantages to use in different problems.

I hope you enjoyed:) See you my next post.

References

--

--