Python Lists

Lists are one of the most important and widely used data structures in Python. They allow you to store multiple values in a single variable, and they are ordered, mutable, and flexible.
Lists are used everywhere—from basic programs to real-world applications.

What Is a List?

A list is a collection that:
  • Is ordered
  • Is mutable (can be changed)
  • Allows duplicate values
  • Can store different data types
python
numbers = [1, 2, 3, 4]
print(numbers)
python
mixed = [1, "Python", 3.14, True]
print(mixed)

Creating a List

Using Square Brackets

python
fruits = ["apple", "banana", "cherry"]
print(fruits)
python
empty_list = []
print(empty_list)

Using list() Constructor

python
numbers = list((1, 2, 3))
print(numbers)
python
letters = list("Python")
print(letters)

Access List Items (Indexing)

List items are indexed starting from 0.
python
colors = ["red", "green", "blue"]
print(colors[0])
python
print(colors[2])
python
print(colors[-1])

Change List Items (Mutable Nature)

python
numbers = [1, 2, 3]
numbers[0] = 10
print(numbers)
python
colors = ["red", "green", "blue"]
colors[1] = "yellow"
print(colors)

List Length

python
numbers = [1, 2, 3, 4]
print(len(numbers))
python
print(len([]))

Check Item Exists

python
fruits = ["apple", "banana", "cherry"]
print("apple" in fruits)
python
print("mango" not in fruits)

Loop Through a List

python
colors = ["red", "green", "blue"]

for color in colors:
    print(color)
python
for i in range(len(colors)):
    print(colors[i])

Add Items to a List

append() – Add at the End

python
numbers = [1, 2, 3]
numbers.append(4)
print(numbers)

insert() – Add at Specific Index

python
numbers = [1, 3, 4]
numbers.insert(1, 2)
print(numbers)

Remove Items from a List

remove() – Remove by Value

python
fruits = ["apple", "banana", "cherry"]
fruits.remove("banana")
print(fruits)

pop() – Remove by Index

python
fruits = ["apple", "banana", "cherry"]
fruits.pop(1)
print(fruits)
python
fruits.pop()

del Keyword

python
numbers = [1, 2, 3]
del numbers[1]
print(numbers)

List Slicing

python
numbers = [0, 1, 2, 3, 4, 5]
print(numbers[1:4])
python
print(numbers[:3])
python
print(numbers[::2])

Copy a List (Important)

Wrong Way (Reference Copy)

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

print(a)

Correct Way (Actual Copy)

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

print(a)
print(b)
python
c = list(a)

Sort a List

python
numbers = [3, 1, 4, 2]
numbers.sort()
print(numbers)
python
numbers.sort(reverse=True)

Reverse a List

python
numbers = [1, 2, 3]
numbers.reverse()
print(numbers)

List Methods (Quick Overview)

MethodDescription
append()Add item
insert()Insert at index
remove()Remove value
pop()Remove index
sort()Sort list
reverse()Reverse list
copy()Copy list
clear()Remove all items
count()Count occurrences
index()Find index

List with Mixed Data Types

python
data = [10, "Python", True, 3.14]
print(data)

Common Mistakes

Modifying List While Iterating

python
nums = [1, 2, 3]

for n in nums:
    nums.remove(n)

print(nums)

Summary

  • Lists store multiple values
  • Ordered and mutable
  • Support indexing and slicing
  • Allow duplicates
  • Rich set of methods
  • Very commonly used in Python programs

Practice

  • Create a list of numbers and modify it
  • Add and remove elements
  • Copy a list safely
  • Sort and reverse a list