Data Structure in Python
Tags: python, algorithms, datastructures, programming
Published: 2023-08-27 02:23:49

Python is a versatile and dynamic programming language that offers a range of built-in data structures. These data structures allow developers to organize and manipulate data efficiently, providing the basis for building high-performance algorithms. In this article, we will delve into some of the most commonly used data structures in Python.

**Lists:**
Lists in Python are highly versatile data structures that let you organize a collection of items in a specific order, regardless of their type. The great thing about these lists is that they can be changed, allowing you to modify their contents even after they have been created.

Example:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xieqllz6trljc9r35nez.png)
The list named "my_list" has a combination of various data types such as integers 1, 2, and 3, the string "hello", and the boolean value True. Lists are collections of items that can be modified and hold elements of different types. It is possible to access, modify, add, and remove elements within a list.

**Tuples:**
Tuples, like lists, are collections of items in a specific order. However, unlike lists, tuples cannot be altered once they are created, making them immutable. Tuples are frequently utilized to represent predetermined sets of values.

Example:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xcor197n35bmncc3quec.png)
You have created a tuple named my_tuple with four elements in this code, which are the integers 1, 2, and 3, and the string "world". Tuples are defined using parentheses ( ) and the elements inside the tuple are separated by commas.

**Dictionaries:**

Associative arrays, also known as dictionaries or maps, allow for the storing of key-value pairs. They provide quick access to values by utilizing unique keys. Dictionaries are useful for tasks that involve converting one set of values into another.

Example:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h5m3z991z9x9fj91wqxh.png)
My_dict is a dictionary that stores information using key-value pairs. Each key such as "name", "age", and "city" is associated with a corresponding value like "Alice", 30, and "New York". By using dictionaries, one can retrieve values quickly using their keys. That makes them perfect for organizing and accessing structured data. In this particular case, the dictionary stores information about a person's name, age, and city.


**Sets:**

Collections of unique elements are called sets. These sets are especially helpful when you want to remove duplicates from a list or check for membership in a constant amount of time.

Example:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/erc5ptdizq8rorpmhvfm.png)
In the set called "my_set", there are five unique values: the integers 1, 2, 3, 4, and 5. Sets are collections that do not have a specific order and eliminate duplicates, guaranteeing that each element only appears once. Sets are useful when working with separate values or performing set operations, such as intersection, union, and difference.

**Stacks and Queues:**

Python may not have dedicated classes for stacks and queues, but you can easily create them using lists. Lists come equipped with useful functions that allow you to replicate the behavior of both a stack (Last-In-First-Out) and a queue (First-In-First-Out).

Example (Stack):

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hfvltzz8v6kzgylr1km4.png)
To create a stack data structure, start by initializing an empty list called "stack". Use the "append()" method to add values to the stack. For example, "stack.append(1)" adds the value 1 to the top of the stack. Adding "stack.append(2)" places 2 on top of 1. To retrieve the top item from the stack, use the "pop()" method. This removes and retrieves the top item, which in this case is 2. Store the retrieved value in a variable called "top_item".

**Linked Lists:**

Python allows the use of custom classes to create linked lists. Each node in these lists contains data and a reference to the next node. Linked lists are highly efficient for insertion and deletion operations, making them very useful.

Example:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yysgijrtaomxq4xqb7dh.png)
To represent individual nodes in a linked list, you can define a class called Node. Each node contains data and a reference to the next node in the list. The constructor for the Node class is the __init__ method, which initializes a new node with the provided data. The data attribute of the node is set to the value passed when creating the node. Additionally, the next attribute is initialized as None, indicating that the node doesn't yet have a reference to the next node in the list. To create nodes with specific data values, you can use the syntax Node(value). For example, Node(1) creates a node named node1 with data value 1. To link nodes together, you can set the next attribute of one node to point to the other node. For instance, node1.next = node2 creates a link between node1 and node2.

**Conclusion:**

For developers, Python's built-in data structures can be highly beneficial when dealing with various computational issues. Understanding the distinct features and appropriate usage of each structure can assist in making informed decisions about which one to implement in different situations. Whether working on simple scripts or intricate applications, mastering these structures can enhance the ability to write efficient and effective Python code.