Day 7: Lists and Tuples in Python

Welcome to Day 7 of the 30 Days of Python Challenge!

Christina Susan Jacob
3 min readAug 27, 2024

Today, we’ll be diving into lists and tuples — two of Python’s most versatile and commonly used data structures. These structures allow you to store and manipulate collections of data efficiently.

1. Lists

A list is an ordered, mutable (changeable) collection of items. Lists can hold items of different data types, including integers, strings, and even other lists.

Creating a List:

# Example of a list
my_list = [1, 2, 3, 4, 5]

Accessing List Elements: You can access elements in a list using their index. Remember that Python uses zero-based indexing.

# Accessing elements
print(my_list[0]) # Output: 1
print(my_list[-1]) # Output: 5 (last element)

Modifying a List: Since lists are mutable, you can modify their elements.

# Modifying elements
my_list[0] = 10
print(my_list) # Output: [10, 2, 3, 4, 5]

List Operations:

  • Append: Add an item to the end of the list.
  • Insert: Add an item at a specific index.
  • Remove: Remove an item by value.
  • Pop: Remove an item by index.
  • Sort: Sort the list in ascending order.

Example:

my_list.append(6)
my_list.insert(1, 15)
my_list.remove(4)
popped_item = my_list.pop(2)
my_list.sort()
print(my_list) # Output after operations: [10, 15, 3, 5, 6]
print(popped_item) # Output: 3

Slicing Lists: You can extract parts of a list using slicing.

# Slicing
subset = my_list[1:4]
print(subset) # Output: [15, 3, 5]

2. Tuples

A tuple is similar to a list, but it is immutable (unchangeable). Once you create a tuple, you cannot modify its contents.

Creating a Tuple:

# Example of a tuple
my_tuple = (1, 2, 3, 4, 5)

Accessing Tuple Elements: Like lists, you can access tuple elements using indices.

# Accessing elements
print(my_tuple[0]) # Output: 1
print(my_tuple[-1]) # Output: 5

Why Use Tuples? Tuples are useful when you want to create a collection of items that should not change throughout the program. They are also faster than lists due to their immutability.

Tuple Operations:

  • You can concatenate tuples using the + operator.
  • You can repeat tuples using the * operator.
  • Tuples support slicing like lists.

Example:

new_tuple = my_tuple + (6, 7)
repeated_tuple = my_tuple * 2
print(new_tuple) # Output: (1, 2, 3, 4, 5, 6, 7)
print(repeated_tuple) # Output: (1, 2, 3, 4, 5, 1, 2, 3, 4, 5)

3. Key Differences Between Lists and Tuples

  • Mutability: Lists are mutable, while tuples are immutable.
  • Performance: Tuples can be more efficient due to their immutability.
  • Use Cases: Use lists when you need a dynamic collection that can change. Use tuples for static collections.

Exercise: Working with Lists and Tuples

Task 1: Create a list of your favorite movies and perform the following operations:

  • Add a new movie to the list.
  • Insert a movie at the second position.
  • Remove the last movie from the list.
  • Print the final list.
movies = ["Inception", "The Matrix", "Interstellar"]
movies.append("The Dark Knight")
movies.insert(1, "Fight Club")
movies.pop()
print(movies) # Output: ['Inception', 'Fight Club', 'The Matrix', 'Interstellar']

Task 2: Create a tuple representing a point in 3D space (x, y, z). Write a function that calculates the distance of this point from the origin (0, 0, 0).

import math
def distance_from_origin(point):
return math.sqrt(point[0]**2 + point[1]**2 + point[2]**2)
point = (3, 4, 5)
distance = distance_from_origin(point)
print("Distance from origin:", distance) # Output: Distance from origin: 7.0710678118654755

Task 3: Given a list of numbers, write a script that finds the minimum and maximum numbers in the list using built-in functions.

numbers = [34, 1, 23, 67, 89, 2]
min_number = min(numbers)
max_number = max(numbers)
print("Minimum number:", min_number) # Output: Minimum number: 1
print("Maximum number:", max_number) # Output: Maximum number: 89

Reflection and What’s Next

Well done on completing Day 7! Lists and tuples are foundational data structures in Python that you’ll use frequently in your programming journey. Understanding their differences and when to use each will make your code more effective and efficient.

Tomorrow, we’ll explore dictionaries and sets, which are essential for working with key-value pairs and unique collections.

Stay Tuned for Day 8: Dictionaries and Sets in Python!

--

--