Python / Python Data Structures
Introduction to Python Lists and Tuples
This tutorial will provide a comprehensive introduction to Python's list and tuple data structures. You will learn how to create, access, modify, and manipulate lists and tuples i…
Section overview
5 resourcesIntroduces built-in data structures such as lists, tuples, dictionaries, and sets.
1. Introduction
The goal of this tutorial is to provide a comprehensive introduction to Python's list and tuple data structures. By the end of this tutorial, you will be able to create, access, modify, and manipulate lists and tuples in Python.
As prerequisites, you should have a basic understanding of Python syntax and data types.
2. Step-by-Step Guide
2.1 Lists
In Python, a list is a mutable, or changeable, ordered sequence of elements. Each element or value that is inside a list is called an item. Lists are defined by having values between square brackets [].
# Defining a list
my_list = [1, 2, 3, "python", 4.5]
You can access list items by referring to the index number, with the first index being 0.
# Accessing list items
print(my_list[0]) # Output: 1
print(my_list[3]) # Output: "python"
2.2 Tuples
A tuple is similar to a list in that it is an ordered sequence of elements. However, tuples are immutable, meaning they cannot be changed after they are created. Tuples are defined by having values between parentheses ().
# Defining a tuple
my_tuple = (1, 2, 3, "python", 4.5)
Like lists, you can access tuple items by referring to the index number.
# Accessing tuple items
print(my_tuple[0]) # Output: 1
print(my_tuple[3]) # Output: "python"
3. Code Examples
3.1 List Manipulation
# Creating a list
fruits = ["apple", "banana", "cherry"]
# Adding an item to the end of the list
fruits.append("orange")
print(fruits) # Output: ['apple', 'banana', 'cherry', 'orange']
# Removing an item from the list
fruits.remove("banana")
print(fruits) # Output: ['apple', 'cherry', 'orange']
3.2 Tuple Manipulation
# Creating a tuple
fruits = ("apple", "banana", "cherry")
# Attempting to change a tuple (this will cause an error)
fruits[0] = "orange" # TypeError: 'tuple' object does not support item assignment
4. Summary
In this tutorial, we've covered the basics of lists and tuples in Python. We've learned how to create these data structures, access their elements, and manipulate lists.
For further learning, consider exploring more complex operations on lists and tuples, such as list comprehensions and tuple unpacking. Additional resources include the Python documentation on lists and tuples.
5. Practice Exercises
-
Create a list of your favorite movies. Then, add a new movie to the list and print the updated list.
-
Create a tuple of your favorite books. Then, try to remove a book from the tuple (you should get an error).
-
Create a list of numbers. Write a Python program to replace the last item in the list with a new number.
Here are the solutions for the exercises:
movies = ["The Godfather", "The Dark Knight", "Pulp Fiction"]
movies.append("Inception")
print(movies) # Output: ['The Godfather', 'The Dark Knight', 'Pulp Fiction', 'Inception']
books = ("1984", "To Kill a Mockingbird", "The Great Gatsby")
books[0] = "Moby Dick" # TypeError: 'tuple' object does not support item assignment
numbers = [1, 2, 3, 4, 5]
numbers[-1] = 10
print(numbers) # Output: [1, 2, 3, 4, 10]
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article