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…

Tutorial 1 of 5 5 resources in this section

Section overview

5 resources

Introduces 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

  1. Create a list of your favorite movies. Then, add a new movie to the list and print the updated list.

  2. Create a tuple of your favorite books. Then, try to remove a book from the tuple (you should get an error).

  3. 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.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Word Counter

Count words, characters, sentences, and paragraphs in real-time.

Use tool

MD5/SHA Hash Generator

Generate MD5, SHA-1, SHA-256, or SHA-512 hashes.

Use tool

JSON Formatter & Validator

Beautify, minify, and validate JSON data.

Use tool

CSV to JSON Converter

Convert CSV files to JSON format and vice versa.

Use tool

Color Palette Generator

Generate color palettes from images.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI 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 article

AI 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 article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help