Python Data Structure Basics
Python is one of the most beginner-friendly and powerful programming languages. One of the main reasons behind its popularity is its rich collection of built-in data structures. These structures make it easy to store, organize, and manipulate data efficiently.
In this blog, weโll explore the five fundamental Python data structures:
๐ Strings
๐ Lists
๐ Tuples
๐ Dictionaries
๐ Sets
Weโll also look at common functions and operations used with each.
๐ค 1. Strings
A string is a sequence of characters enclosed within quotes (single ' '
, double " "
, or triple quotes ''' '''
/ """ """
).
Example:
name = "Python Programming"
โ Common String Operations:
Operation | Example | Output |
---|---|---|
Access characters | name[0] | 'P' |
Slicing | name[0:6] | 'Python' |
Length | len(name) | 18 |
Convert to upper case | name.upper() | 'PYTHON PROGRAMMING' |
Convert to lower case | name.lower() | 'python programming' |
Replace text | name.replace("Python", "Java") | 'Java Programming' |
Split string | name.split(" ") | ['Python', 'Programming'] |
Join list into string | " ".join(['Python', 'Rocks']) | 'Python Rocks' |
Note:
Strings are immutable, meaning you cannot change a string once created. You can only create new ones.
๐ 2. Lists
A list is an ordered, mutable (changeable) collection of items. Lists can store multiple data types.
Example:
fruits = ["apple", "banana", "cherry"]
โ Common List Operations:
Operation | Example | Output |
---|---|---|
Access element | fruits[0] | 'apple' |
Append item | fruits.append("orange") | ['apple', 'banana', 'cherry', 'orange'] |
Insert item | fruits.insert(1, "mango") | ['apple', 'mango', 'banana', 'cherry'] |
Remove item | fruits.remove("banana") | ['apple', 'mango', 'cherry'] |
Pop item | fruits.pop() | Removes last element |
Sort list | fruits.sort() | Alphabetical order |
Reverse list | fruits.reverse() | Reverses order |
Length | len(fruits) | 3 |
Note:
Lists are mutable, so you can modify them after creation.
๐งฉ 3. Tuples
A tuple is an ordered and immutable collection of elements. Once created, you cannot change, add, or remove items.
Example:
dimensions = (10, 20, 30)
โ Common Tuple Operations:
Operation | Example | Output |
---|---|---|
Access element | dimensions[0] | 10 |
Length | len(dimensions) | 3 |
Count occurrences | dimensions.count(10) | 1 |
Find index | dimensions.index(20) | 1 |
Note:
Tuples are often used for fixed data, like coordinates or configuration values.
๐ง 4. Dictionaries
A dictionary stores data in key-value pairs, similar to a real-world dictionary. Keys must be unique and immutable, while values can be of any type.
Example:
person = {
"name": "Alice",
"age": 25,
"city": "Mumbai"
}
โ Common Dictionary Operations:
Operation | Example | Output |
---|---|---|
Access value | person["name"] | 'Alice' |
Add new key-value | person["country"] = "India" | Adds new entry |
Update value | person["age"] = 26 | Updates age |
Remove key | person.pop("city") | Removes 'city' |
Get all keys | person.keys() | dict_keys(['name', 'age', 'country']) |
Get all values | person.values() | dict_values(['Alice', 26, 'India']) |
Get all items | person.items() | Returns list of key-value pairs |
Clear dictionary | person.clear() | {} |
Note:
Dictionaries are unordered (Python 3.6+ maintains insertion order, but logically considered unordered).
๐งฎ 5. Sets
A set is an unordered collection of unique items. Sets are great for removing duplicates and performing mathematical operations like union and intersection.
Example:
numbers = {1, 2, 3, 4, 5}
โ Common Set Operations:
Operation | Example | Output |
---|---|---|
Add element | numbers.add(6) | {1, 2, 3, 4, 5, 6} |
Remove element | numbers.remove(3) | {1, 2, 4, 5, 6} |
Union | `{1, 2, 3} | {3, 4, 5}` |
Intersection | {1, 2, 3} & {3, 4, 5} | {3} |
Difference | {1, 2, 3} - {3, 4, 5} | {1, 2} |
Check membership | 2 in numbers | True |
Length | len(numbers) | 5 |
Note:
Sets are unordered and unindexed, so you canโt access elements by index.
๐งฐ 6. Quick Comparison Table
Feature | String | List | Tuple | Dictionary | Set |
---|---|---|---|---|---|
Ordered | โ | โ | โ | โ (mostly) | โ |
Mutable | โ | โ | โ | โ | โ |
Indexed | โ | โ | โ | โ | โ |
Allows Duplicates | โ | โ | โ | โ (unique keys) | โ |
Syntax | " " | [ ] | ( ) | {key: value} | { } |
๐งโ๐ป Conclusion
Pythonโs data structures are the backbone of efficient programming.
- Use strings for text.
- Use lists when you need ordered, changeable data.
- Use tuples for fixed sequences.
- Use dictionaries for key-value mapping.
- Use sets for unique, unordered data.
Understanding and practicing these fundamentals is the first step toward mastering Python programming and building complex systems like data analysis pipelines, trading bots, or web applications.