Python Built-in Data Structures


Python provides several built-in data structures that are essential for storing and organizing data efficiently. Each data structure has unique properties and is suited for different types of tasks. This tutorial covers the primary built-in data structures in Python: lists, tuples, sets, and dictionaries.

Table of Contents

  1. Lists
  2. Tuples
  3. Sets
  4. Dictionaries
  5. Conclusion

1. Lists


A list is an ordered, mutable collection of items. Lists are versatile and can hold items of different data types, including numbers, strings, and even other lists. They are defined by placing items inside square brackets [], separated by commas.

Creating Lists

# Creating a list
my_list = [1, 2, 3, "apple", "banana"]

Accessing List Items

You can access list items using their index. Indexing starts at 0, and negative indices can be used to access items from the end of the list.

# Accessing list items
print(my_list[0])  # Output: 1
print(my_list[3])  # Output: apple

# Negative indexing
print(my_list[-1])  # Output: banana

Modifying Lists

Lists are mutable, so you can change their items.

# Modifying list items
my_list[1] = "orange"
print(my_list)  # Output: [1, 'orange', 3, 'apple', 'banana']

List Methods

Lists come with various built-in methods for adding, removing, and manipulating items.

# Adding items to a list
print(my_list)  # Output: [1, 'orange', 3, 'apple', 'banana', 'cherry']

# Removing items from a list
print(my_list)  # Output: [1, 'orange', 3, 'banana', 'cherry']

# Sorting a list
numbers = [4, 2, 9, 1]
print(numbers)  # Output: [1, 2, 4, 9]

# Reversing a list
print(numbers)  # Output: [9, 4, 2, 1]

2. Tuples


A tuple is an ordered, immutable collection of items. Tuples are similar to lists but cannot be modified after creation. They are defined by placing items inside parentheses (), separated by commas.

Creating Tuples

# Creating a tuple
my_tuple = (1, 2, 3, "apple", "banana")

Accessing Tuple Items

You can access tuple items using their index.

# Accessing tuple items
print(my_tuple[0])  # Output: 1
print(my_tuple[3])  # Output: apple

# Negative indexing
print(my_tuple[-1])  # Output: banana

Tuple Methods

Tuples support a limited number of methods due to their immutability, such as count() and index().

# Counting occurrences of a value
print(my_tuple.count("apple"))  # Output: 1

# Finding the index of a value
print(my_tuple.index("banana"))  # Output: 4

3. Sets


A set is an unordered collection of unique items. Sets are mutable, but they do not allow duplicate elements. They are defined by placing items inside curly braces {}, separated by commas, or by using the set() function.

Creating Sets

# Creating a set
my_set = {1, 2, 3, "apple", "banana"}

# Creating a set using the set() function
my_set = set([1, 2, 3, "apple", "banana"])

Accessing Set Items

You cannot access set items using an index, but you can loop through the set items or check if an item exists in the set.

# Looping through set items
for item in my_set:

# Checking if an item exists in the set
print("apple" in my_set)  # Output: True

Set Methods

Sets come with various built-in methods for adding, removing, and performing set operations.

# Adding items to a set

# Removing items from a set

# Performing set operations
set1 = {1, 2, 3}
set2 = {3, 4, 5}

# Union
print(set1.union(set2))  # Output: {1, 2, 3, 4, 5}

# Intersection
print(set1.intersection(set2))  # Output: {3}

# Difference
print(set1.difference(set2))  # Output: {1, 2}

4. Dictionaries


A dictionary is an unordered collection of key-value pairs. Each key must be unique and immutable, while the values can be of any data type. Dictionaries are defined by placing key-value pairs inside curly braces {}, separated by commas, with a colon : separating keys and values.

Creating Dictionaries

# Creating a dictionary
my_dict = {"name": "Alice", "age": 25, "city": "New York"}

Accessing Dictionary Items

You can access dictionary items using their keys.

# Accessing dictionary items
print(my_dict["name"])  # Output: Alice
print(my_dict["age"])   # Output: 25

Modifying Dictionaries

Dictionaries are mutable, so you can change their keys and values.

# Modifying dictionary items
my_dict["age"] = 26
print(my_dict)  # Output: {'name': 'Alice', 'age': 26, 'city': 'New York'}

# Adding new key-value pairs
my_dict["email"] = "[email protected]"
print(my_dict)  # Output: {'name': 'Alice', 'age': 26, 'city': 'New York', 'email': '[email protected]'}

Dictionary Methods

Dictionaries come with various built-in methods for manipulating key-value pairs.

# Getting all keys
print(my_dict.keys())  # Output: dict_keys(['name', 'age', 'city', 'email'])

# Getting all values
print(my_dict.values())  # Output: dict_values(['Alice', 26, 'New York', '[email protected]'])

# Removing a key-value pair
print(my_dict)  # Output: {'name': 'Alice', 'age': 26, 'email': '[email protected]'}

5. Conclusion

Python's built-in data structures—lists, tuples, sets, and dictionaries—are powerful tools for organizing and manipulating data. Understanding the properties and methods of each data structure is essential for writing efficient and readable Python code. This tutorial provided an overview of these data structures, including how to create, access, and manipulate them using various built-in methods.