Python Lists, Tuples, and Ranges Explained

When working with large amounts of data in Python, it’s not practical to create a separate variable for each piece of data. For example, if you wanted to record temperatures for an entire year, you would need 365 variables (day1 = 65, day2 = 68, ...). That’s messy and inefficient.

Instead, Python provides sequence data structures—like lists, tuples, and ranges—that allow you to store and organize data much more effectively. Let’s break them down.


Lists

A list in Python is a collection of elements enclosed in square brackets [], separated by commas. Lists are very flexible—they can hold integers, floats, strings, booleans, or even a mix of data types.

Examples:

integer_list = [1, 3, 6, 1, 4, 6]       # List of integers
grocery_list = ["apples", "milk", "chicken", "eggs"]   # List of strings
random_list = [4.5, 6, "llama", True]   # Mixed types

Indexing

Each item in a list has an index. Python uses zero-based indexing, meaning the first element is index 0, the second is 1, and so on.

Example:

print(grocery_list[0])   # "apples"
print(grocery_list[2])   # "chicken"

Python also supports negative indexing. The last element is at index -1, the second-to-last is -2, etc.

Example:

print(random_list[-1])   # True
print(integer_list[-2])  # 4

Slicing

Slicing allows you to extract a portion of a list. The syntax is:

list_name[start:end:step]
  • start → index where slicing begins (inclusive). Defaults to 0.
  • end → index where slicing ends (exclusive).
  • step → how many items to skip. Defaults to 1.

Example:

my_list = [1, 4, 7, 3, 9, 3, 5, 6, 2, 8]

print(my_list[1:4])      # [4, 7, 3]
print(my_list[:5:2])     # [1, 7, 9]

Slicing also supports negative indices.


Mutability

Lists are mutable, meaning you can change their contents after creation.

Example:

grocery_list = ["apples", "milk", "chicken", "eggs"]
grocery_list[1] = "orange juice"

print(grocery_list)  # ["apples", "orange juice", "chicken", "eggs"]

Length

Use len() to find how many elements are in a list:

print(len(grocery_list))  # 4

List Operations

  • Concatenation:
list_1 = [1]
list_2 = [2]
combined_list = list_1 + list_2
print(combined_list)   # [1, 2]
  • Repetition:
new_list = list_2 * 5
print(new_list)   # [2, 2, 2, 2, 2]

⚠️ Note: You cannot subtract or divide lists.


Common List Methods

  • list.append(x) → adds x to the end
  • list.pop(i) → removes and returns element at index i (default is last element)
  • list.remove(x) → removes the first occurrence of value x
  • list.index(x) → returns the index of the first occurrence of x
  • list.count(x) → counts how many times x appears
  • list.reverse() → reverses the list in place

Tuples

A tuple is very similar to a list but is defined with parentheses () instead of brackets. Tuples can hold elements of any type and support indexing, slicing, and length operations just like lists.

Example:

tuple1 = (1, 2, 3, 4)

Key Difference: Immutability

Tuples are immutable. Once created, their contents cannot be changed. This makes them useful for data that should remain constant or protected from accidental modification.

If you need to “change” a tuple, you must create a new one.


Tuple Unpacking

Tuples can be unpacked into individual variables:

tuple1 = (1, 2, 3, 4)
a1, a2, a3, a4 = tuple1
print(a2)   # 2

Tuple Operations

  • Concatenation:
tuple1 = (1,)
tuple2 = (2,)
new_tuple = tuple1 + tuple2
print(new_tuple)   # (1, 2)

⚠️ Notice the trailing comma in (1,). Without it, Python would just treat (1) as an integer in parentheses, not a tuple.

  • Repetition:
print(tuple1 * 5)   # (1, 1, 1, 1, 1)

Tuple Methods

  • tuple.index(x) → returns index of the first occurrence of x
  • tuple.count(x) → counts how many times x appears

Ranges

The range() function generates a sequence of integers. Ranges are often used in loops but can also be converted into lists or tuples.

Syntax

range(start, stop, step)
  • start → starting value (inclusive, default = 0)
  • stop → ending value (exclusive)
  • step → increment between values (default = 1)

Examples:

days = range(1, 366)    # 1 through 365
print(days[-1])         # 365

Summary

  • Lists → ordered, mutable collections of elements ([]).
  • Tuples → ordered, immutable collections of elements (()).
  • Ranges → generate sequences of integers with range(start, stop, step).

Lists are best for flexible data storage where values may change. Tuples are ideal when you want fixed, unchangeable data. Ranges are efficient for representing sequences of numbers, especially in loops.