---
title: Data-Structures-in-Python
author: sumendar
date: '2023-02-10'
slug: Data-Structures-in-Python
categories:
- python
- data structures
tags:
- basics
subtitle: with reallife examples
---
In Python, a data structures is a way of organizing and storing data that enables efficient access and modification of the data. Here are some of the most commonly used data structures in Python along with real-life examples and their use cases:
List
List: A list is an ordered, mutable, and heterogeneous collection of elements. Each element in a list can be of any type, including another list. Here’s an example of a list that stores the names of different fruits: css
```{code-cell} ipython3 fruits = [‘apple’, ‘banana’, ‘cherry’, ‘date’, ‘strawberry’] colors = [“Green”, “Red”, “black”] print(fruits[:4]) print(colors)
Use case: Lists are commonly used to store collections of items such as a shopping list, a list of tasks to complete, or a list of names.
# Tuple
**Tuple:** A tuple is similar to a list but is immutable, meaning its elements cannot be changed once it is created. Here's an example of a tuple that stores the name, age, and gender of a person:
python
person = ('Rakesh', 32, 'Male')
**Use case:** Tuples are commonly used to store fixed and unchanging data, such as date and time, coordinates, or to return multiple values from a function.
# Dictionary
**Dictionary:** A dictionary is an unordered collection of key-value pairs, where each key is unique and used to retrieve its corresponding value. Here's an example of a dictionary that stores the names and ages of different people:
python
# Set
```{code-cell} ipython3
people = {'Harish': 32, 'Shankar': 28, 'Sumeeth': 45}
Use case: Dictionaries are commonly used to store data that can be represented as key-value pairs, such as a map of city names to their populations or a map of names to email addresses.
Set: A set is an unordered collection of unique elements, meaning it does not allow duplicate elements. Here’s an example of a set that stores the unique elements of a list: scss
```{code-cell} ipython3 numbers = [1, 2, 3, 4, 5, 5, 4, 3, 2, 1]
```{code-cell} ipython3
unique_numbers = set(numbers)
print(unique_numbers)
Use case: Sets are commonly used to store collections of unique elements and to perform operations such as union, intersection, and difference between sets.
String
String: A string is a sequence of characters. Strings are immutable(couldnot able to update inplace), meaning their elements cannot be changed once they are created. Here’s an example of a string: makefile
{code-cell} ipython3
text = "Hello, world!" #string
changed = text.replace("Hello", "Data")
print(changed)
Use case: Strings are commonly used to store and manipulate text data, such as sentences, names, or paragraphs.
Sources & References
https://www.geeksforgeeks.org/python-lists/?ref=lbp