Skip to main content
  • Python is available now. All other compilers are coming soon.

    Python
  • Popular Tutorials

    • Getting Started With Python
    • Python if Statement
    • while Loop in Python
    • Python Lists
    • Dictionaries in Python
    All Python Tutorials

    Reference Materials

    Built-in FunctionsList MethodsDictionary MethodsString Methods
    View all

Online Compilers

  • Python Compiler
  • Python Playground
  • Python Projects
  • Exercises

Tutorials

  • Learn Python
  • Python for Beginners
  • Variables and Types
  • Python Strings
  • Python Lists
  • Python If Else
  • Python For Loops
  • Python Functions
  • Python Classes

Examples

  • Python Examples
  • Hello World
  • Add Two Numbers
  • Even or Odd
  • Factorial
  • Fibonacci
  • Prime Check
  • Palindrome Check

Company

  • About
  • Contact
  • Privacy Policy
  • Terms of Use
  • Disclaimer

© 2026 ILoveCompiler. All rights reserved.

  1. Home
  2. Python Tutorial
  3. Python Lists: Creation, Indexing, and Iteration

Data structures · Tutorial 14

Python Lists: Creation, Indexing, and Iteration

Store ordered collections in lists, access items by position, iterate over them, and use slicing and unpacking.

Open the compilerPractice exercises
beginner9 min readUpdated November 19, 20264 learning goals
On this page
  1. Length and membership
  2. Indexing
  3. Slicing
  4. Changing items
  5. Iteration
  6. List comprehensions
  7. Nested lists
  8. Copying and references
  9. Practice

What you will learn

  • Create and inspect lists
  • Index, slice, and iterate over lists
  • Use list comprehensions for simple transformations
  • Recognise how references affect copying
Before this lesson: Python for Loops, Python Strings: Creation, Indexing, and Slicing

A list is an ordered, mutable collection. It can hold values of any type, including other lists.

Python
fruits = ["apple", "banana", "cherry"]
numbers = [3, 1, 4, 1, 5]
mixed = [1, "two", 3.0, True, None]
empty = []

The square brackets create the list. Commas separate the items.

Length and membership#

Python
fruits = ["apple", "banana", "cherry"]

print(len(fruits))          # 3
print("banana" in fruits)   # True
print("grape" in fruits)    # False

len() returns the number of top-level items. Membership uses equality comparisons.

Indexing#

Python
fruits = ["apple", "banana", "cherry"]

print(fruits[0])    # apple
print(fruits[1])    # banana
print(fruits[-1])   # cherry
print(fruits[-2])   # banana

A negative index counts from the end. Out-of-range indexes raise IndexError.

Slicing#

Slices return a new list:

Python
numbers = [0, 1, 2, 3, 4, 5]

print(numbers[1:4])   # [1, 2, 3]
print(numbers[:3])    # [0, 1, 2]
print(numbers[3:])    # [3, 4, 5]
print(numbers[::2])   # [0, 2, 4]
print(numbers[::-1])  # [5, 4, 3, 2, 1]

The stop index is excluded, just like string slicing.

Changing items#

Lists are mutable, so you can assign to an index or a slice:

Python
fruits = ["apple", "banana", "cherry"]
fruits[1] = "blueberry"
fruits[0:1] = ["apricot", "avocado"]

print(fruits)
Expected output

['apricot', 'avocado', 'blueberry', 'cherry']

The slice assignment can change the list length.

Iteration#

Python
scores = [72, 85, 91]

for score in scores:
    print(score)

Use enumerate() when you also need the position:

Python
for index, score in enumerate(scores, start=1):
    print(f"Student {index}: {score}")

List comprehensions#

A list comprehension creates a new list from an iterable:

Python
numbers = [1, 2, 3, 4, 5, 6]

squares = [number ** 2 for number in numbers]
evens = [number for number in numbers if number % 2 == 0]

print(squares)
print(evens)
Expected output

[1, 4, 9, 16, 25, 36] [2, 4, 6]

Keep comprehensions short. If the expression needs several steps, a normal loop is clearer.

Nested lists#

Python
grid = [
    [1, 2, 3],
    [4, 5, 6],
]

print(grid[1][2])  # 6

for row in grid:
    for value in row:
        print(value, end=" ")
    print()

A nested list is often used for matrices, but each inner list is independent only if you create them separately.

Copying and references#

Assignment does not copy a list:

Python
a = [1, 2, 3]
b = a
b.append(4)

print(a)
Expected output

[1, 2, 3, 4]

Both names refer to the same list. Make a shallow copy with a.copy(), a[:], or list(a). For nested lists, use copy.deepcopy() when you need independent inner objects.

Avoid this repeated-list trap

[[0] * 3] * 2 creates two references to the same inner list. Change one row and both change. Build independent rows with a comprehension: [[0] * 3 for _ in range(2)].

Practice#

Create a list of five temperatures. Print the highest and lowest values, the average, and a new list containing only temperatures above the average. Then reverse the original list and print it.

Previous tutorialPython break, continue, and Loop elseNext tutorialPython List Methods: append, insert, remove, sort, and More

Related tutorials

beginner

Python List Methods: append, insert, remove, sort, and More

Modify lists with append, extend, insert, remove, pop, sort, reverse, and understand which methods return None.

8 min read
beginner

Python Tuples: Immutable Sequences and Unpacking

Use tuples for fixed groups of values, unpack them cleanly, and know when a tuple is a better choice than a list.

7 min read

On this page

  1. Length and membership
  2. Indexing
  3. Slicing
  4. Changing items
  5. Iteration
  6. List comprehensions
  7. Nested lists
  8. Copying and references
  9. Practice

Run the examples

Every Python block has a Run in compiler button that opens the code in a new tab.