A list in Python is a way to keep many values in one variable. Instead of writing mark1, mark2 and mark3, you write one list called marks that holds all of them in order.
This page shows how to create a list, read items, take slices, add and remove items and loop over a list. Every example has its output. If you are new to Python, start with if else statements and come back here.
How to create a list
Write the items inside square brackets, separated by commas.
marks = [72, 85, 64, 90]
names = ["Anjali", "Kiran", "Ravi"]
mixed = [101, "Meena", 8.5, True]
empty = []
print(marks)
print(names)
print(mixed)
print(empty)
print(len(marks))
Output:
[72, 85, 64, 90]
['Anjali', 'Kiran', 'Ravi']
[101, 'Meena', 8.5, True]
[]
4
len() gives the number of items. A list can mix types, as mixed shows, but most real lists hold one kind of value, such as all marks or all names.
Indexing: read one item
Each item has a position number called an index. The first index is 0, not 1. Python also allows negative indexes, which count from the end.
Item: 72 85 64 90
Index: 0 1 2 3
Negative index: -4 -3 -2 -1
marks = [72, 85, 64, 90]
print(marks[0])
print(marks[2])
print(marks[-1])
print(marks[-2])
Output:
72
64
90
64
| Expression | Value | Why |
|---|---|---|
marks[0] |
72 | First item |
marks[2] |
64 | Third item |
marks[-1] |
90 | Last item |
marks[-2] |
64 | Second from the end |
marks[4] |
Error | There is no index 4 in a 4-item list |
Asking for an index that does not exist stops the program with IndexError: list index out of range.
You can also change an item by its index:
marks = [72, 85, 64, 90]
marks[2] = 70
print(marks)
Output:
[72, 85, 70, 90]
List slicing: take a part of a list
A slice gives you a new list with a range of items. The form is list[start:stop]. The item at start is included and the item at stop is not.
nums = [10, 20, 30, 40, 50, 60]
print(nums[1:4])
print(nums[:3])
print(nums[3:])
print(nums[-2:])
print(nums[::2])
print(nums[::-1])
Output:
[20, 30, 40]
[10, 20, 30]
[40, 50, 60]
[50, 60]
[10, 30, 50]
[60, 50, 40, 30, 20, 10]
| Slice | Meaning | Result |
|---|---|---|
nums[1:4] |
Index 1 up to, not including, 4 | [20, 30, 40] |
nums[:3] |
From the start up to index 3 | [10, 20, 30] |
nums[3:] |
From index 3 to the end | [40, 50, 60] |
nums[-2:] |
Last two items | [50, 60] |
nums[::2] |
Every second item | [10, 30, 50] |
nums[::-1] |
Whole list reversed | [60, 50, 40, 30, 20, 10] |
The third number in [start:stop:step] is the step. A step of -1 walks backwards. Slicing never changes the original list, and it does not raise an error if stop is past the end.
Adding items: append, insert and extend
fruits = ["apple", "banana"]
fruits.append("mango")
print(fruits)
fruits.insert(1, "grapes")
print(fruits)
fruits.extend(["kiwi", "orange"])
print(fruits)
Output:
['apple', 'banana', 'mango']
['apple', 'grapes', 'banana', 'mango']
['apple', 'grapes', 'banana', 'mango', 'kiwi', 'orange']
| Method | What it does |
|---|---|
append(x) |
Adds x at the end |
insert(i, x) |
Puts x at index i and shifts the rest right |
extend(other) |
Adds every item of another list at the end |
Removing items: remove, pop, del and clear
fruits = ["apple", "grapes", "banana", "mango", "banana"]
fruits.remove("banana")
print(fruits)
last = fruits.pop()
print(last, fruits)
first = fruits.pop(0)
print(first, fruits)
del fruits[0]
print(fruits)
fruits.clear()
print(fruits)
Output:
['apple', 'grapes', 'mango', 'banana']
banana ['apple', 'grapes', 'mango']
apple ['grapes', 'mango']
['mango']
[]
| Method | Removes | Returns the item? |
|---|---|---|
remove(x) |
The first item equal to x |
No |
pop() |
The last item | Yes |
pop(i) |
The item at index i |
Yes |
del list[i] |
The item at index i |
No |
clear() |
Every item | No |
Notice that remove("banana") deleted only the first "banana". If the value is not in the list, remove() raises ValueError, so check with in first when you are not sure.
Looping over a list
A for loop visits each item in order.
marks = [72, 85, 64, 90]
total = 0
for m in marks:
total = total + m
print("Added", m, "total is now", total)
print("Average:", total / len(marks))
Output:
Added 72 total is now 72
Added 85 total is now 157
Added 64 total is now 221
Added 90 total is now 311
Average: 77.75
When you also need the index, use enumerate():
names = ["Anjali", "Kiran", "Ravi"]
for i, name in enumerate(names):
print(i, name)
Output:
0 Anjali
1 Kiran
2 Ravi
Other useful list methods and functions
nums = [40, 10, 30, 10, 20]
print(30 in nums)
print(nums.count(10))
print(nums.index(30))
print(min(nums), max(nums), sum(nums))
print(sorted(nums))
print(nums)
nums.sort()
print(nums)
nums.reverse()
print(nums)
Output:
True
2
2
10 40 110
[10, 10, 20, 30, 40]
[40, 10, 30, 10, 20]
[10, 10, 20, 30, 40]
[40, 30, 20, 10, 10]
| Tool | What it gives |
|---|---|
x in list |
True if x is in the list |
list.count(x) |
How many times x appears |
list.index(x) |
Index of the first x |
min(), max(), sum() |
Smallest, largest and total |
sorted(list) |
A new sorted list. The original stays the same |
list.sort() |
Sorts the list itself and returns None |
list.reverse() |
Reverses the list itself |
Common mistake: Writing
nums = nums.sort().sort()changes the list in place and returnsNone, sonumsbecomesNone. Writenums.sort()on its own line, or usenums = sorted(nums).
Small program: keep only passing marks
This puts together a list, a loop, an if and append.
marks = [72, 28, 85, 34, 64, 90]
passed = []
for m in marks:
if m >= 35:
passed.append(m)
print("Passed:", passed)
print("Count:", len(passed))
Output:
Passed: [72, 85, 64, 90]
Count: 4
Once this pattern feels easy, you can write the same thing in one line with list comprehension: passed = [m for m in marks if m >= 35].
Copying a list the right way
a = [1, 2, 3]
b = a
c = a.copy()
a.append(4)
print(a)
print(b)
print(c)
Output:
[1, 2, 3, 4]
[1, 2, 3, 4]
[1, 2, 3]
b = a does not make a copy. Both names point to the same list, so a change through a shows up in b. Use a.copy() or a[:] when you want a separate list.
Lists compared with other Python collections
| Collection | Example | Ordered | Can change | Use it for |
|---|---|---|---|---|
| List | [72, 85, 64] |
Yes | Yes | A sequence of items you will add to or edit |
| Tuple | (17.38, 78.48) |
Yes | No | Fixed groups of values |
| Dictionary | {"name": "Ravi", "marks": 85} |
Yes | Yes | Looking up values by a key |
| Set | {"java", "python"} |
No | Yes | Unique items and fast membership checks |
When you need to look up a student's marks by name instead of by position, a list is the wrong tool. Use a Python dictionary instead. For a full side-by-side, read list vs tuple.
Quick revision
- A list holds many values in order:
marks = [72, 85, 64]. - Indexes start at 0.
-1is the last item. list[start:stop]includesstartand excludesstop.- Add with
append,insertandextend. - Remove with
remove(by value),popanddel(by index). - Loop with
for item in list, orenumeratewhen you need the index. sort()changes the list.sorted()returns a new one.
Python lists work like arrays in other languages, with extra features built in. To see how arrays work in C and Java, read what is an array. Then practise list questions from the Python interview questions page.
Frequently Asked Questions
What is a list in Python in simple words?
Can a Python list hold different data types?
What is the difference between append and extend?
What is the difference between remove and pop?
What is the difference between a list and a tuple?
Want to Build Your Career in Data Science with AI?
Join CodeBegun and train with working industry engineers — See the Data Science course in Hyderabad

