In the previous lesson, you learned how to create Python lists and store multiple values in a single variable. Once a list has been created, the next important step is learning how to access and modify its elements. Python provides a powerful indexing system that allows you to retrieve any element quickly using its position.
List indexing is one of the most fundamental concepts in Python programming. Whether you are building web applications, analyzing data, automating tasks, or developing machine learning models, you will frequently access list elements using indexes.
In this lesson, you will learn what list indexing is, why it is important, how positive and negative indexing work, how to modify list elements using indexes, and how indexing is used in real-world applications.
After completing this lesson, you will be able to:
List indexing is the process of accessing an element from a list using its position. Every element in a Python list has an index number.
Python uses zero-based indexing, meaning the first element has an index of 0, the second element has an index of 1, and so on.
list_name[index]
fruits = ["Apple", "Banana", "Orange"]
print(fruits[0])
Output
Apple
The element at index 0 is Apple.
Indexing allows you to access individual elements without processing the entire list.
It is useful for:
Positive indexing starts from the beginning of the list.
| Index | Element |
|---|---|
| 0 | Apple |
| 1 | Banana |
| 2 | Orange |
| 3 | Mango |
fruits = ["Apple", "Banana", "Orange", "Mango"]
print(fruits[0])
print(fruits[2])
Output
Apple
Orange
The first element is accessed using index 0, while the third element uses index 2.
Negative indexing starts from the end of the list.
| Negative Index | Element |
|---|---|
| -1 | Mango |
| -2 | Orange |
| -3 | Banana |
| -4 | Apple |
fruits = ["Apple", "Banana", "Orange", "Mango"]
print(fruits[-1])
print(fruits[-3])
Output
Mango
Banana
Negative indexing is especially useful when you need the last element without knowing the list size.
Lists can contain multiple data types, and indexing works the same way for all elements.
employee = ["Rahul", 101, 65000.50, True]
print(employee[0])
print(employee[2])
Output
Rahul
65000.5
Lists are mutable, so elements can be changed using their indexes.
colors = ["Red", "Green", "Blue"]
colors[1] = "Yellow"
print(colors)
Output
['Red', 'Yellow', 'Blue']
The element at index 1 is replaced with Yellow.
You can modify different elements individually using their indexes.
marks = [75, 82, 91]
marks[0] = 80
marks[2] = 95
print(marks)
Output
[80, 82, 95]
Suppose a school stores student names in a list.
students = ["Rahul", "Priya", "Amit", "Neha"]
print("Class Monitor:", students[0])
print("Last Student:", students[-1])
Output
Class Monitor: Rahul
Last Student: Neha
Indexing allows the program to retrieve specific records quickly.
Consider the following program.
languages = ["Python", "Java", "C++", "JavaScript"]
print(languages[2])
Execution Steps
languages.2."C++") is retrieved.Output
C++
The first element is always stored at index 0, not 1.
numbers = [10, 20, 30]
print(numbers[5])
Output
IndexError: list index out of range
Positive indexes count from the beginning, while negative indexes count from the end.
You can only update elements that are already present in the list.
Python follows zero-based indexing for lists, strings, tuples, and most other sequences.
In this section, you learned what Python list indexing is and why it is essential for accessing and modifying list elements. You explored positive and negative indexing, updated elements using indexes, accessed different data types, and examined practical examples and execution flow. You also learned about common beginner mistakes such as invalid indexes and zero-based indexing. In the next section, you will learn Python List Slicing, including slicing syntax, start and end indexes, default values, step values, reversing lists, and practical slicing examples.
In the previous section, you learned how to access and modify individual list elements using positive and negative indexing. While indexing retrieves a single element, Python also allows you to extract multiple elements at once using a feature called list slicing.
List slicing is one of the most useful operations in Python because it allows you to create a new list containing a selected range of elements. It is widely used in data analysis, web development, machine learning, and automation.
In this section, you will learn how list slicing works, understand slicing syntax, use positive and negative indexes, apply step values, reverse lists, and explore practical examples.
List slicing is the process of extracting a portion of a list by specifying a range of indexes.
Unlike indexing, which returns a single element, slicing returns a new list containing multiple elements.
list_name[start : stop : step]
The three slicing parameters are:
Suppose you have the following list.
fruits = ["Apple", "Banana", "Orange", "Mango", "Grapes"]
print(fruits[1:4])
Output
['Banana', 'Orange', 'Mango']
The element at index 1 is included, while the element at index 4 is excluded.
If the starting index is omitted, Python automatically starts from index 0.
numbers = [10, 20, 30, 40, 50]
print(numbers[:3])
Output
[10, 20, 30]
Python begins from the first element and stops before index 3.
If the ending index is omitted, Python continues until the last element.
numbers = [10, 20, 30, 40, 50]
print(numbers[2:])
Output
[30, 40, 50]
You can create a shallow copy of a list using slicing.
colors = ["Red", "Green", "Blue"]
new_colors = colors[:]
print(new_colors)
Output
['Red', 'Green', 'Blue']
This creates a new list containing all the elements of the original list.
Negative indexes can also be used in slicing.
fruits = ["Apple", "Banana", "Orange", "Mango", "Grapes"]
print(fruits[-4:-1])
Output
['Banana', 'Orange', 'Mango']
Negative slicing starts counting from the end of the list.
The step parameter determines the interval between selected elements.
numbers = [1, 2, 3, 4, 5, 6, 7, 8]
print(numbers[::2])
Output
[1, 3, 5, 7]
Every second element is selected.
numbers = [1, 2, 3, 4, 5, 6, 7, 8]
print(numbers[1::2])
Output
[2, 4, 6, 8]
The slicing starts from index 1 and selects every second element.
A negative step value reverses the order of the list.
numbers = [10, 20, 30, 40, 50]
print(numbers[::-1])
Output
[50, 40, 30, 20, 10]
The step value of -1 reads the list from the last element to the first.
letters = ["A", "B", "C", "D", "E", "F", "G"]
print(letters[::2])
Output
['A', 'C', 'E', 'G']
Suppose a teacher wants to display only the first five students from a class list.
students = [
"Rahul",
"Priya",
"Amit",
"Neha",
"Rohan",
"Simran",
"Karan"
]
print(students[:5])
Output
['Rahul', 'Priya', 'Amit', 'Neha', 'Rohan']
List slicing makes it easy to retrieve only the required records.
Consider the following program.
numbers = [10, 20, 30, 40, 50]
result = numbers[1:4]
print(result)
Execution Steps
1.4 (exclusive).result.Output
[20, 30, 40]
The stop index is always excluded from the sliced result.
Indexing returns a single element, while slicing returns a new list.
If the step is omitted, Python automatically uses a value of 1.
A step value of 0 is not allowed and raises a ValueError.
Slicing creates a new list without changing the original list.
In this section, you learned how Python list slicing allows you to extract multiple elements efficiently. You explored slicing syntax, start and stop indexes, default values, negative slicing, step values, copying lists, reversing lists, and extracting alternate elements. You also examined practical examples, execution flow, and common beginner mistakes. In the next section, you will learn advanced indexing and slicing techniques, including membership operators, the len() function, traversing lists using indexes, combining indexing with slicing, performance tips, and real-world applications.
In the previous section, you learned how to extract portions of a list using slicing. Python also provides several additional techniques that make indexing and slicing even more useful. You can check whether an element exists in a list, determine the total number of elements, traverse a list using indexes, and combine indexing with slicing to solve practical programming problems.
These techniques are widely used in data analysis, software development, automation, and machine learning because they allow programmers to efficiently retrieve and process list data.
in and not inPython provides membership operators to determine whether a value exists inside a list.
infruits = ["Apple", "Banana", "Orange"]
print("Banana" in fruits)
Output
True
The in operator returns True because Banana exists in the list.
not infruits = ["Apple", "Banana", "Orange"]
print("Mango" not in fruits)
Output
True
The not in operator returns True because Mango is not present in the list.
The built-in len() function returns the total number of elements in a list.
len(list_name)
students = ["Rahul", "Priya", "Amit", "Neha"]
print(len(students))
Output
4
The list contains four elements.
You can use the range() function with len() to access both indexes and values.
fruits = ["Apple", "Banana", "Orange"]
for i in range(len(fruits)):
print(i, fruits[i])
Output
0 Apple
1 Banana
2 Orange
This technique is useful when both the index and value are required.
Indexing and slicing can be used together in the same program.
numbers = [10, 20, 30, 40, 50, 60]
print(numbers[0])
print(numbers[2:5])
print(numbers[-1])
Output
10
[30, 40, 50]
60
The program retrieves the first element, a slice of elements, and the last element.
The step parameter allows you to skip elements while slicing.
numbers = [1,2,3,4,5,6,7,8,9,10]
print(numbers[::3])
Output
[1, 4, 7, 10]
Negative indexing and slicing make it easy to retrieve elements from the end of a list.
numbers = [10,20,30,40,50,60,70]
print(numbers[-3:])
Output
[50, 60, 70]
numbers = [10,20,30,40,50]
print(numbers[1:-1])
Output
[20, 30, 40]
This technique is commonly used when processing data while ignoring boundary values.
students = [
"Rahul",
"Priya",
"Amit",
"Neha",
"Rohan",
"Simran",
"Karan"
]
top_students = students[:5]
print(top_students)
Output
['Rahul', 'Priya', 'Amit', 'Neha', 'Rohan']
sales = [1200, 1350, 980, 1450, 1600, 1750, 1900]
latest_sales = sales[-3:]
print(latest_sales)
Output
[1600, 1750, 1900]
Negative slicing is useful for retrieving the most recent records.
products = ["Laptop", "Mouse", "Keyboard", "Monitor"]
if "Mouse" in products:
print("Available")
else:
print("Not Available")
Output
Available
Consider the following program.
numbers = [10,20,30,40,50]
result = numbers[1:4]
print(result)
Execution Steps
1.4 (exclusive).result.Output
[20, 30, 40]
len() instead of manually counting elements.The original list remains unchanged after slicing.
The stop index is always excluded.
in with ==in checks for membership, while == compares entire objects.
Negative indexes must still refer to existing elements.
Keep slicing expressions simple and readable whenever possible.
In this section, you learned advanced Python list indexing and slicing techniques. You explored membership operators, the len() function, traversing lists using indexes, combining indexing with slicing, retrieving alternate elements, extracting records from the end of a list, and solving practical real-world problems. You also reviewed best practices, performance tips, and common beginner mistakes. In the final section, you will review the complete lesson with a summary, key takeaways, frequently asked questions, coding exercises, interview questions, a mini project, and a preview of the next lesson on Python Nested Lists: Complete Guide for Beginners.
In this lesson, you learned how Python list indexing and slicing allow you to access, retrieve, and manipulate list elements efficiently. Indexing helps you work with individual elements, while slicing enables you to extract multiple elements as a new list.
You began by learning about positive and negative indexing, understanding how Python uses zero-based indexing, and modifying list elements using their indexes. You also explored real-world examples where indexing is used to retrieve specific records quickly.
Next, you learned how list slicing works using the start, stop, and step parameters. You discovered how to extract portions of a list, copy lists, reverse lists, skip elements, and use negative slicing to access data from the end of a list.
Finally, you explored advanced techniques such as checking membership using in and not in, determining the length of a list using len(), traversing lists using indexes, combining indexing with slicing, and applying these concepts in practical examples.
List indexing and slicing are essential skills used in data analysis, automation, web development, machine learning, and general Python programming.
list[start:stop:step].stop index is excluded from the result.len() function returns the number of elements in a list.in and not in operators check whether an element exists in a list.List indexing is the process of accessing an element using its position in the list.
The first element is stored at index 0.
Negative indexing accesses elements from the end of the list. The last element has an index of -1.
List slicing extracts a portion of a list and returns it as a new list.
The syntax is list[start:stop:step].
No. The stop index is always excluded.
Use a negative step value: list[::-1].
len() function return?It returns the total number of elements in a list.
in operator?It checks whether an element exists in a list.
No. Slicing creates a new list while leaving the original list unchanged.
in operator.len().step parameter?len() function work?Create a Python program that analyzes student marks using indexing and slicing.
Your program should:
len().========== STUDENT MARKS ANALYZER ==========
Marks
[78, 85, 91, 73, 88, 95, 81, 69]
First Student : 78
Last Student : 69
Top Five Marks
[78, 85, 91, 73, 88]
Last Three Marks
[95, 81, 69]
Reversed List
[69, 81, 95, 88, 73, 91, 85, 78]
95 Exists : True
Total Students : 8
============================================
Congratulations! You have successfully learned Python List Indexing and Slicing. You now understand how to access individual elements, extract portions of a list, modify data, reverse lists, use step values, check membership, and determine the length of a list efficiently.
In the next lesson, you will learn Python Nested Lists: Complete Guide for Beginners. You will discover how to create lists inside other lists, access nested elements, modify nested data, work with two-dimensional lists (matrices), and solve practical real-world problems using nested lists.