Struggling with basic data structures in Python?
03 May 2020Article also published in Medium.
It’s normal for any beginner to land into confusion because of the similarity between the basic data structures used in python but you can get a good understanding of them if you compare them and use them. Don’t just read the theory part, it’s important to code and use them to gain required confidence.**
Data structures are an essential part of any programming language, be it C/C++, Java, Python, etc. Now, what is a data structure? In simple words, it’s a structure or a way to organize your given data. Your data can be Python has primarily four data structures namely: List, Tuple, Set, Dictionary. Now consider we have 4 data inputs namely: Apple, Mango, 23, 32. We can use these data structures to organize our data, starting off :
List:
In this data structure, we store our data inside [] brackets. List is ordered *which means when you print your data then you would get it in an ordered manner in the console. You *can have duplicate members in your List.
List in python
You can access the data from the index number (note that index number can be negative as well) and change it, i.e List objects are mutable.
List are mutable
Similarly, you can add, remove, sort, append, etc; data in your List using the functions in Lists. For example to append a data in List you may use append() function.
append() in List
Tuple:
In this data structure, we store our data inside () brackets. A tuple is ordered. You can have duplicate members in your Tuple.
Tuple in python
You can access the data from the index number but cannot change it, i.e Tuple objects are immutable.
Tuple objects are immutable
Similarly, you may use other functions in Tuple. For example to count the number of the appearance of your data in Tuple you may use count() function.
count() in Tuple
Set:
In this data structure, we store our data inside {} brackets. The set is unordered. You cannot have duplicate members in your Set.
Sets are unordered in python
Duplicates are not shown in Set
You cannot access the data from the index number and change it, i.e Set objects are immutable.
Set objects are immutable
Similarly, you may use other functions in Set. For example to pop a data may use pop() function.
pop() in Set
Dictionary:
In this data structure, we store our data in pairs i.e (key, item) inside {} brackets. Dictionary is unordered. You cannot have duplicate keys in your Dictionary but you may have duplicate items in your dictionary.
Dictionary is unordered
Duplicate keys in Dictionary
Duplicate items in Dictionary
You can access the data via keys and change it, i.e Dictionary objects are mutable. You can also use get() function to access the data from keys.
Duplicate objects are mutable
Similarly, you may use other functions in Dictionary. For example to get data may use get() function.
get() in Dictionary
Hence this is a brief overview of the basic data structures that exist in python.
To gain familiarity with all these data structures make sure that you use them when required and practice them!