Python Data Structures
Python provides several built-in data structures that allow you to store and organize data efficiently. In this post, we will explore the most commonly used data structures in Python: lists, tuples, sets, and dictionaries.
Table of Contents
1. Lists
Lists are ordered collections of items that are mutable, meaning you can change their content after creation. Lists can hold items of different data types.
Creating a List
Example:
python
# Creating a list
fruits = ["apple", "banana", "cherry"]
print(fruits)
Output:
css
['apple', 'banana', 'cherry']
Accessing List Elements
You can access list elements by their index, with the first element having an index of 0.
Example:
python
# Accessing elements
print(fruits[0]) # Output: apple
print(fruits[1]) # Output: banana
print(fruits[2]) # Output: cherry
Modifying List Elements
Lists are mutable, so you can modify their elements.
Example:
python
# Modifying elements
fruits[1] = "blueberry"
print(fruits) # Output: ['apple', 'blueberry', 'cherry']
List Methods
Lists come with several built-in methods for common operations.
Example:
python
# Adding elements
fruits.append("date")
print(fruits) # Output: ['apple', 'blueberry', 'cherry', 'date']
# Removing elements
fruits.remove("blueberry")
print(fruits) # Output: ['apple', 'cherry', 'date']
# Finding length of list
print(len(fruits)) # Output: 3

2. Tuples
Tuples are ordered collections of items that are immutable, meaning you cannot change their content after creation. Like lists, tuples can hold items of different data types.
Creating a Tuple
Example:
python
# Creating a tuple
colors = ("red", "green", "blue")
print(colors)
Output:
arduino
('red', 'green', 'blue')
Accessing Tuple Elements
You can access tuple elements by their index.
Example:
python
# Accessing elements
print(colors[0]) # Output: red
print(colors[1]) # Output: green
print(colors[2]) # Output: blue
Immutability of Tuples
Tuples cannot be modified after creation.
Example:
python
# Trying to modify a tuple (will raise an error)
# colors[1] = "yellow" # Uncommenting this line will cause a TypeError
3. Sets
Sets are unordered collections of unique items. They are useful for storing items without duplicates and for performing mathematical set operations.
Creating a Set
Example:
python
# Creating a set
unique_numbers = {1, 2, 3, 4, 5}
print(unique_numbers)
Output:
{1, 2, 3, 4, 5}
Adding and Removing Elements
Sets are mutable, so you can add and remove elements.
Example:
python
# Adding elements
unique_numbers.add(6)
print(unique_numbers) # Output: {1, 2, 3, 4, 5, 6}
# Removing elements
unique_numbers.remove(3)
print(unique_numbers) # Output: {1, 2, 4, 5, 6}
Set Operations
Sets support operations like union, intersection, and difference.
Example:
python
set_a = {1, 2, 3}
set_b = {3, 4, 5}
# Union
print(set_a | set_b) # Output: {1, 2, 3, 4, 5}
# Intersection
print(set_a & set_b) # Output: {3}
# Difference
print(set_a - set_b) # Output: {1, 2}
4. Dictionaries
Dictionaries are unordered collections of key-value pairs. They are useful for storing data that needs to be quickly retrieved using a unique key.
Creating a Dictionary
Example:
python
# Creating a dictionary
student = {"name": "Alice", "age": 25, "city": "New York"}
print(student)
Output:
arduino
{'name': 'Alice', 'age': 25, 'city': 'New York'}
Accessing Dictionary Elements
You can access dictionary elements by their keys.
Example:
python
# Accessing elements
print(student["name"]) # Output: Alice
print(student["age"]) # Output: 25
print(student["city"]) # Output: New York
Modifying Dictionary Elements
Dictionaries are mutable, so you can modify their elements.
Example:
python
# Modifying elements
student["age"] = 26
print(student) # Output: {'name': 'Alice', 'age': 26, 'city': 'New York'}
Dictionary Methods
Dictionaries come with several built-in methods for common operations.
Example:
python
# Adding a new key-value pair
student["course"] = "Data Science"
print(student) # Output: {'name': 'Alice', 'age': 26, 'city': 'New York', 'course': 'Data Science'}
# Removing a key-value pair
student.pop("city")
print(student) # Output: {'name': 'Alice', 'age': 26, 'course': 'Data Science'}
# Getting all keys and values
print(student.keys()) # Output: dict_keys(['name', 'age', 'course'])
print(student.values()) # Output: dict_values(['Alice', 26, 'Data Science'])
Conclusion
In this post, we covered Python’s built-in data structures: lists, tuples, sets, and dictionaries. Understanding these data structures is crucial for efficient data storage and manipulation. In the next post, we will explore file handling in Python, including reading from and writing to files. Stay tuned!
Check out our Trending Courses Demo Playlist
Data Analytics with Power Bi and Fabric |
Could Data Engineer |
Data Analytics With Power Bi Fabic |
AWS Data Engineering with Snowflake |
Azure Data Engineering |
Azure & Fabric for Power bi |
Full Stack Power Bi |
Kick Start Your Career With Our Data Job
Social Media channels
► KSR Datavizon Website :- https://www.datavizon.com
► KSR Datavizon LinkedIn :- https://www.linkedin.com/company/datavizon/
► KSR Datavizon You tube :- https://www.youtube.com/c/KSRDatavizon
► KSR Datavizon Twitter :- https://twitter.com/ksrdatavizon
► KSR Datavizon Instagram :- https://www.instagram.com/ksr_datavision
► KSR Datavizon Face book :- https://www.facebook.com/KSRConsultingServices
► KSR Datavizon Playstore :- https://play.google.com/store/apps/details?id=com.datavizon.courses&hl=en-IN
► KSR Datavizon Appstore :- https://apps.apple.com/in/app/ksr-datavizon/id1611034268
Most Commented