Python Data Structure
🌿What is Data Structure?
Data Structures are a way of organizing data so that it can be accessed more efficiently depending on the situation.
Data Structures are fundamentals of any programming language around which a program is built.
Python helps to learn the fundamentals of these data structures in a simpler way as compared to other programming languages
🌿Why Data Structures?
Data organization: Data structures help you organize and store data in a structured manner.
Efficiency: Choosing the right data structure can improve the efficiency of your code.
Data retrieval and manipulation: Data structures provide methods for common data operations.
Search and retrieval: Some data structures offer fast lookup and retrieval of values.
Memory management: Data structures help manage memory efficiently, reducing consumption.
🌿Types of Data Structure
Python provides a variety of built-in data structures to store and manipulate data efficiently. Some of the most commonly used data structures in Python include:
1.Lists
Lists are ordered collections of elements, and they can contain elements of different types. Lists are mutable, which means you can change their content by adding, removing, or modifying elements.
List items are ordered, changeable, and allow duplicate values. List items are indexed, the first item has an index [0]
, the second item has an index [1]
etc
my_list = [1, 0, 8, "Hello", 5.0]
print(my_list)
Note: Python file run by following the way:
syntax: python3 file_Name.py
2.Tuples
Tuples are similar to lists but are immutable, meaning their elements cannot be changed after creation**(unchangeable)**. They are often used to represent fixed collections of items.
Tuples are written with round brackets.
Let's create a program for tuple & run it
my_tuple = (1, 2, 3, "Ramesh")
print(my_tuple)
3.Sets
Sets are unordered collections of unique elements. They are commonly used for tasks like removing duplicates from a list or testing membership.
In a set, the order of the elements is undefined; it may return the changed sequence of the element. The set is created by using a built-in function set(), or a sequence of elements is passed in the curly braces and separated by the comma.
set to avoid the repetitive numbers.
my_set = {1, 2, 3, 3, 4}
print(my_set)
4.Dictionaries
Dictionaries are key-value pairs. They are used to store data in an associative manner, where you can look up values by their associated keys.
my_dict = { "name": "vyankatesh", "age": "22", "city": "Pune", "tech": "DevOps"}
print("My Fav Tech Stack is", my_dict["tech"])
5.Strings
Strings are used to represent text and are considered sequences of characters. They are immutable in Python.
my_string = "Welcome to India"
print(my_string)
6.Arrays
Arrays are used to store sequences of elements of the same data type. They are part of the array
module in Python.
import array
my_array = array.array("i", [1, 2, 3, 4, 5])
print(my_array[0])
print(my_array[2])
print(my_array[4])
7.Stacks and Queues
You can implement stacks and queues using lists or specialized libraries such as queue
or deque
from the collections
module
8.Linked Lists
Python does not have a built-in linked list, but you can implement it using classes and references.
🌿Difference Between List, Tuple & Set
Characteristic | List | Tuple | Set |
Mutability | Mutable - can be changed, added, or removed after creation. | Immutable - cannot be changed after creation. | Mutable - can be changed, but elements must be unique. |
Syntax | Defined using square brackets, e.g., [1, 2, 3] . | Defined using parentheses, e.g., (1, 2, 3) . | Defined using curly braces, e.g., {1, 2, 3} or set([1, 2, 3]) . |
Duplicate Elements | Can contain duplicate elements. | Can contain duplicate elements. | Does not allow duplicate elements. |
Order | Ordered - elements are stored in a specific order, and you can access them by index. | Ordered - elements are stored in a specific order, and you can access them by index. | Unordered - no specific order for elements. You can't access elements by index. |
Iteration | Supports iteration through elements using a for loop. | Supports iteration through elements using a for loop. | Supports iteration through elements using a for loop, but order is not guaranteed. |
Use Cases | Commonly used for dynamic collections that can change over time, such as to-do lists. | Used for storing data that should remain constant, like coordinates or configuration settings. | Useful for working with collections of unique, unordered elements, e.g., removing duplicates from a list. |
Consider One example for sorting:
Create a List of cloud service providers. Write a program to add Digital Ocean
to the list of cloud_providers and sort the list in alphabetical order.
cloudproviders=["AWS","GCP","Azure"]
print(cloudproviders)
print("Adding the name of Cloud as DIGITAL OCEAN")
cloudproviders.append("DIGITAL OCEAN")
cloudproviders.sort()
print(cloudproviders)
Congratulations 🎉 ! You learn about Data Structure with a practical approach.
I hope you enjoy the blog post!
If you do, please show your support by giving it a like ❤, leaving a comment 💬, and spreading the word 📢 to your friends and colleagues 😊