Lists and Tuples

Understanding sequence types in Python

30 minutesBeginner

Learning Objectives

  • Understand the concept of sequences in Python
  • Master list operations and methods
  • Learn about tuple immutability
  • Work with nested data structures

Lists and Tuples in Python

Introduction to Sequences

Python provides several built-in sequence types, with lists and tuples being the most commonly used. A sequence is an ordered collection of items that can be indexed and sliced.

Key characteristics of sequences:

  • Items are ordered (maintain their position)
  • Can be accessed by index
  • Can contain any type of data
  • Can be nested (contain other sequences)

Key Points:

  • Sequences maintain order of elements
  • Elements can be accessed by index
  • Support common operations like slicing
  • Can contain mixed data types

Sequence Memory Visualization

Memory Layout

Details

Click on a memory slot to view details

Sequence Indexing

In Python, sequence elements are accessed using indices. Python uses zero-based indexing, meaning:

  • The first element is at index 0
  • The last element is at index length-1
  • Negative indices count from the end (-1 is the last element)

Try accessing elements using both positive and negative indices in the interactive example below.

Key Points:

  • Zero-based indexing
  • Positive indices from start
  • Negative indices from end
  • IndexError if out of range

Array Indexing

Index: 0

apple

Negative Index: -4

Index: 1

banana

Negative Index: -3

Index: 2

orange

Negative Index: -2

Index: 3

grape

Negative Index: -1

Working with Lists

Lists are mutable sequences, meaning they can be modified after creation. Python provides many built-in methods to manipulate lists:

  • Adding elements: append(), insert(), extend()
  • Removing elements: pop(), remove(), clear()
  • Ordering: sort(), reverse()
  • Information: count(), index()

Experiment with these methods in the interactive explorer below.

Key Points:

  • Lists are mutable
  • Many built-in methods available
  • Methods modify in-place
  • Some methods return values

List Methods Explorer

Available Methods

Current List

[1, 2, 3]

History

my_list = [1, 2, 3]

Live List Editor

[0]1int
[1]"hello"str
[2]truebool

Understanding Tuples

Tuples are immutable sequences, meaning they cannot be modified after creation. They are commonly used for:

  • Returning multiple values from functions
  • Creating constant sequences
  • Dictionary keys (when needed)
  • Data integrity (preventing accidental modifications)

Key differences from lists:

  • Use parentheses () instead of square brackets []
  • Cannot be modified after creation
  • Generally more memory efficient
  • Slightly faster than lists

Key Points:

  • Tuples are immutable
  • Used for fixed data
  • More efficient than lists
  • Great for multiple return values

List vs Tuple Comparison

FeatureListTuple
MutabilityMutable - can be modified after creationImmutable - cannot be modified after creation
Methodsappend(), extend(), insert(), remove(), pop(), clear(), sort(), reverse()count(), index()
Item AssignmentSupported - items can be changedNot supported - items cannot be changed

Code Example

List Example

# List example
numbers = [1, 2, 3]
numbers.append(4)      # OK
numbers[0] = 0        # OK
print(numbers)        # [0, 2, 3, 4]

Tuple Example

# Tuple example
numbers = (1, 2, 3)
# numbers.append(4)   # Error!
# numbers[0] = 0     # Error!
print(numbers)        # (1, 2, 3)

Nested Data Structures

Lists and tuples can contain other lists and tuples, creating nested structures. Common uses include:

  • Matrices (lists of lists)
  • Tree structures
  • Complex data hierarchies
  • Game boards

These structures are accessed using multiple indices: matrix[row][column]

Key Points:

  • Can nest lists and tuples
  • Useful for complex data
  • Access with multiple indices
  • Common in real applications

Nested Data Structure Visualizer

matrix:
]
mixed:
]
Click on the arrows (▶) to expand/collapse nested structures
Initializing Python environment...

Lists and Tuples Quiz

Key Takeaways

  • Understanding of sequence types in Python
  • Ability to work with lists and their methods
  • Knowledge of tuple immutability and use cases
  • Skills in handling nested data structures
  • Practice with common sequence operations