When we learn to code in python or any other programming language, we generally start with basic syntax and mathematical operations. However, we cannot create an entire software only with basic programming. In this article, we will discuss various data structures in python that you must learn to be able to create good software.
What is a data structure?
A data structure is a programming construct used to store data. Data structures allow us to store and process data in an efficient manner without much hassle.
Python has many built-in data structures such as List, Set, Tuple, Dictionary, Frozenset, etc. All these data structures have unique features that make them useful in a certain situation. Apart from the inbuilt data structures, there are many data structures such as Stacks, Queues, Deques, Binary Tree, Graph, linked list, HashMap, etc that are used in programs to implement various business and logical functions.
Let us discuss all these python data structures briefly to have an overview so that you can start with them in 2023 and beyond.
Top Built-in Python data structures
Python List
Lists in python are equivalent to arrays in other programming languages like C++ and Java. However, Python lists have the advantage that they can contain elements of different data types. On the other hand, Arrays in C++ or Java can only contain elements of a specific data type. You can define a list in python using square brackets or list() function as shown below.
myList=[1,2,3,4,5]
Lists have no fixed size and you can add as many elements to them as you want. Generally, we add an element to a list using the append() method or the insert() method. The append() method adds the element to the end of the list. On the contrary, we can insert an element at any position in the list using the insert() method.
myList.append(6)#appends 6 at end of the list
myList.insert(2,7)#inserts 7 at index 2
We generally use lists in our programs when we have homogeneous data and we need random access to the data.
Python Tuple
Tuples are immutable equivalents of lists. We can define a tuple using the parentheses as follows.
myTuple=(1,2,3,4,5)
After creating a tuple, we cannot add an element to it or delete an element. So, tuples are used only when we only need random access to the data and we don’t need to modify it.
Python Dictionary
Just like a language dictionary contains words and their meanings, a python dictionary is used to store key-value pairs. To understand how dictionaries work, let us assume that we have to store the name and the age of students in a class. Here, we can associate the age of each student with their name. So, we consider the names of the students as keys and their age as the associated value to the key.
You can create a dictionary using curly braces as shown below.
myDict={1:1, 2:4,3:9}
As you can see, each key-value pair in the dictionary consists of the detail of one student. We call a key-value pair an item. You can add items to the dictionary as shown below.
myDict[4]=16 #adds an item with key 4 and value 16 to the dictionary
To remove items from a dictionary, we use the pop() method. The pop() method, when invoked on a dictionary, takes the key as an input argument and returns the associated value to the key. It also deletes the key-value pair in the process.
myDict.pop(4)
Python Sets
Python sets are used to store immutable objects. In other words, a set can only contain elements like a number, a character, a string, or an immutable container object like a tuple. You can create a set using curly braces as follows.
mySet={1,2,3,4,5}
A set can contain each element only once. You can add an element to the set using the add() method. To delete an element from a set, you can use the remove() method or the discard() method as shown below.
mySet.add(6)
mySet.remove(3)
mySet.discard(2)
Python String
Python strings are used to store text data. We define a string using double quotes or single quotes as shown below.
myStr= 'Codeconquest'
Strings are immutable objects. However, we can concatenate them or slice them to create new strings using various methods provided in the python standard library.
Custom Python Data Structures
Apart from the built-in data structures, we can also define various data structures on our own. Let us discuss some of the custom python data structures.
Linked Lists
Linked lists are linear data structures just like a list. However, elements of a linked list cannot be retrieved randomly using their indices as the elements of a linked list are not stored in a sequential manner. Each element in a linked list consists of an object that points to the next object in the list.
Stacks
Stacks are also linear data structures that follow the Last in First Out (LIFO) principle. Here, we can only access the data that has been entered last into the stack, Just like we can only take the topmost book from a stack of books. You can implement a stack using a list or a linked list.
Queues
Queues are used to implement the First in First Out principle. Here, the data that is entered first into the list will be accessed first, just like queues in real life where the person who enters the queue first gets served first. You can implement a queue using lists or linked lists.
Tree
Trees are non-linear data structures that we use to store and process hierarchical data. It consists of different objects that are called the nodes of the tree and the references between the nodes that are called edges of a tree. A tree doesn’t have a loop in it. When a tree happens to have a loop in it, it is called a graph. You can learn about graphs in this article on graphs in python.
Conclusion
In this article, we have discussed different built-in and custom data structures that you should learn in 2023 and beyond. Learning these data structures will help you learn python in the best way possible as you can use different data structures according to your requirements.
I hope you enjoyed reading this article. Stay tuned for more informative articles.
Happy Learning!
Disclosure of Material Connection: Some of the links in the post above are “affiliate links.” This means if you click on the link and purchase the item, I will receive an affiliate commission. Regardless, I only recommend products or services I use personally and believe will add value to my readers.