DEV Community

Cover image for Python Data Structures: Lists, Tuples, and Dictionaries Explained
Nevil Bhayani
Nevil Bhayani

Posted on • Originally published at yourblog.com

Python Data Structures: Lists, Tuples, and Dictionaries Explained

Python Data Structures Guide

Python offers several built-in data structures that are essential for every developer.

Lists

Lists are ordered, mutable collections:

fruits = ['apple', 'banana', 'orange']
fruits.append('grape')
print(fruits)  # ['apple', 'banana', 'orange', 'grape']
Enter fullscreen mode Exit fullscreen mode

Tuples

Tuples are ordered, immutable collections:

coordinates = (10, 20)
x, y = coordinates
print(f'X: {x}, Y: {y}')
Enter fullscreen mode Exit fullscreen mode

Dictionaries

Dictionaries store key-value pairs:

student = {
    'name': 'John',
    'age': 25,
    'courses': ['Math', 'Science']
}
print(student['name'])  # John
Enter fullscreen mode Exit fullscreen mode

When to Use Each

  • Lists: When you need ordered, changeable data
  • Tuples: For immutable sequences
  • Dictionaries: For key-value relationships

Conclusion

Mastering these data structures is crucial for Python development!

Top comments (0)