In the previous lesson, you learned how to work with Python strings, including escape characters, string operations, and formatting. While strings are useful for storing text, many real-world applications require storing multiple values together. For example, you may need to store the names of students, marks of employees, product prices, or daily temperatures.
Python provides a powerful built-in data structure called a list for storing multiple items in a single variable. Lists are one of the most commonly used data types in Python because they are flexible, ordered, and mutable.
Lists are widely used in web development, data analysis, automation, machine learning, and almost every Python application.
In this lesson, you will learn what Python lists are, why they are useful, how to create them, and how to access their elements using positive and negative indexing.
After completing this lesson, you will be able to:
A list is an ordered collection of items stored inside square brackets ([]).
Each item in a list is called an element. A list can contain values of the same data type or different data types.
list_name = [item1, item2, item3]
fruits = ["Apple", "Banana", "Orange"]
print(fruits)
Output
['Apple', 'Banana', 'Orange']
The list contains three string elements.
Without lists, you would need a separate variable for every value.
student1 = "Rahul"
student2 = "Priya"
student3 = "Amit"
This approach becomes difficult to manage as the number of values increases.
students = ["Rahul", "Priya", "Amit"]
print(students)
Output
['Rahul', 'Priya', 'Amit']
Lists store multiple related values in a single variable, making programs simpler and more organized.
Python lists have several important characteristics.
data = ["Python", 2026, True, 95.5]
print(data)
Output
['Python', 2026, True, 95.5]
Lists are created by placing elements inside square brackets and separating them with commas.
numbers = [10, 20, 30, 40]
print(numbers)
Output
[10, 20, 30, 40]
cities = ["Delhi", "Mumbai", "Chennai"]
print(cities)
Output
['Delhi', 'Mumbai', 'Chennai']
employee = ["Rahul", 101, 55000.75, True]
print(employee)
Output
['Rahul', 101, 55000.75, True]
Each element in a list has an index. Indexing starts from 0.
| Index | Value |
|---|---|
| 0 | Apple |
| 1 | Banana |
| 2 | Orange |
fruits = ["Apple", "Banana", "Orange"]
print(fruits[0])
Output
Apple
Positive indexing starts from the beginning of the list.
colors = ["Red", "Green", "Blue", "Yellow"]
print(colors[1])
print(colors[3])
Output
Green
Yellow
The first element has index 0, the second has index 1, and so on.
Negative indexing starts from the end of the list.
| Negative Index | Value |
|---|---|
| -1 | Yellow |
| -2 | Blue |
| -3 | Green |
| -4 | Red |
colors = ["Red", "Green", "Blue", "Yellow"]
print(colors[-1])
print(colors[-3])
Output
Yellow
Green
Negative indexing is useful when you need to access elements from the end of a list.
Suppose you want to store marks obtained by five students.
marks = [85, 92, 76, 88, 95]
print("First Student:", marks[0])
print("Last Student:", marks[-1])
Output
First Student: 85
Last Student: 95
Lists make it easy to organize and access related data.
Consider the following program.
languages = ["Python", "Java", "C++"]
print(languages[1])
Execution Steps
languages.1."Java", is retrieved.Output
Java
The first element is at index 0, not 1.
Using an index that does not exist raises an IndexError.
numbers = [10, 20, 30]
print(numbers[5])
Output
IndexError: list index out of range
Lists use square brackets ([]), whereas strings use quotation marks.
Every element in a list must be separated by a comma.
Python lists can contain elements of different data types.
In this section, you learned what Python lists are and why they are one of the most important data structures in Python. You explored their characteristics, created lists containing different data types, and learned how to access elements using positive and negative indexing. You also examined real-world examples, followed the execution flow of list indexing, and identified common beginner mistakes. In the next section, you will learn how to slice lists, modify existing elements, add and remove items, check membership, and determine the length of a list using built-in functions.
In the previous section, you learned how to create Python lists and access their elements using positive and negative indexing. Lists become even more powerful because they are mutable, meaning you can modify, add, and remove elements after the list has been created.
Python provides several built-in methods that make working with lists simple and efficient. In this section, you will learn how to slice lists, modify existing elements, add new items, remove unwanted items, check membership, and determine the length of a list.
List slicing allows you to extract a portion of a list by specifying a range of indexes.
list_name[start : end]
The start index is included, while the end index is excluded.
fruits = ["Apple", "Banana", "Orange", "Mango", "Grapes"]
print(fruits[1:4])
Output
['Banana', 'Orange', 'Mango']
numbers = [10, 20, 30, 40, 50]
print(numbers[:3])
Output
[10, 20, 30]
numbers = [10, 20, 30, 40, 50]
print(numbers[2:])
Output
[30, 40, 50]
Since lists are mutable, individual elements can be changed using their index.
colors = ["Red", "Green", "Blue"]
colors[1] = "Yellow"
print(colors)
Output
['Red', 'Yellow', 'Blue']
The second element is replaced with "Yellow".
append()The append() method adds a single element to the end of a list.
list.append(item)
fruits = ["Apple", "Banana"]
fruits.append("Orange")
print(fruits)
Output
['Apple', 'Banana', 'Orange']
insert()The insert() method inserts an element at a specific position.
list.insert(index, item)
numbers = [10, 30, 40]
numbers.insert(1, 20)
print(numbers)
Output
[10, 20, 30, 40]
extend()The extend() method adds all elements from another iterable to the end of the list.
list1 = [1, 2, 3]
list2 = [4, 5, 6]
list1.extend(list2)
print(list1)
Output
[1, 2, 3, 4, 5, 6]
remove()The remove() method removes the first occurrence of a specified value.
fruits = ["Apple", "Banana", "Orange"]
fruits.remove("Banana")
print(fruits)
Output
['Apple', 'Orange']
pop()The pop() method removes and returns an element by its index. If no index is given, it removes the last element.
numbers = [10, 20, 30, 40]
removed = numbers.pop()
print(removed)
print(numbers)
Output
40
[10, 20, 30]
numbers = [10, 20, 30, 40]
numbers.pop(1)
print(numbers)
Output
[10, 30, 40]
delThe del statement deletes an element or an entire list.
numbers = [10, 20, 30, 40]
del numbers[2]
print(numbers)
Output
[10, 20, 40]
clear()The clear() method removes all elements from a list.
cities = ["Delhi", "Mumbai", "Chennai"]
cities.clear()
print(cities)
Output
[]
The in and not in operators check whether an element exists in a list.
fruits = ["Apple", "Banana", "Orange"]
print("Banana" in fruits)
print("Mango" not in fruits)
Output
True
True
The len() function returns the number of elements in a list.
students = ["Rahul", "Priya", "Amit", "Neha"]
print(len(students))
Output
4
Suppose a teacher wants to update a list of student names.
students = ["Rahul", "Priya", "Amit"]
students.append("Neha")
students.remove("Priya")
print(students)
Output
['Rahul', 'Amit', 'Neha']
Lists make it easy to manage changing collections of data.
Consider the following program.
numbers = [10, 20, 30]
numbers.append(40)
print(numbers)
Execution Steps
append() method is called.40 is added to the end of the list.Output
[10, 20, 30, 40]
remove() for an Indexremove() deletes by value, not by index.
pop()An invalid index raises an IndexError.
append() Adds Only One ItemTo add multiple items, use extend().
append() and insert()append() adds an element at the end, while insert() places it at a specified position.
clear() Deletes the List Variableclear() removes the elements but the list still exists.
In this section, you learned how to work with Python lists by slicing them, modifying elements, adding items using append(), insert(), and extend(), removing elements with remove(), pop(), del, and clear(), checking membership, and finding the length of a list. You also explored practical examples, execution flow, and common beginner mistakes. In the next section, you will learn advanced list methods such as sort(), reverse(), copy(), count(), index(), nested lists, and practical real-world applications.
In the previous section, you learned how to create, modify, add, and remove elements from Python lists. Python also provides several built-in list methods that make it easy to sort, reverse, copy, count, and search list elements. These methods are commonly used in data analysis, automation, web applications, and general programming.
In this section, you will explore the most useful list methods, learn about nested lists, and apply these concepts through practical real-world examples.
sort() MethodThe sort() method arranges list elements in ascending order by default.
numbers = [50, 10, 30, 20, 40]
numbers.sort()
print(numbers)
Output
[10, 20, 30, 40, 50]
numbers = [50, 10, 30, 20, 40]
numbers.sort(reverse=True)
print(numbers)
Output
[50, 40, 30, 20, 10]
The reverse=True argument sorts the list in descending order.
reverse() MethodThe reverse() method reverses the order of elements in a list.
colors = ["Red", "Green", "Blue"]
colors.reverse()
print(colors)
Output
['Blue', 'Green', 'Red']
This method changes only the order of elements and does not sort them.
copy() MethodThe copy() method creates a shallow copy of a list.
list1 = ["Python", "Java", "C++"]
list2 = list1.copy()
print(list2)
Output
['Python', 'Java', 'C++']
Changes made to list2 do not affect list1.
count() MethodThe count() method returns the number of times an element appears in a list.
numbers = [10, 20, 10, 30, 10]
print(numbers.count(10))
Output
3
index() MethodThe index() method returns the index of the first occurrence of a specified element.
fruits = ["Apple", "Banana", "Orange"]
print(fruits.index("Banana"))
Output
1
If the element is not found, Python raises a ValueError.
A nested list is a list that contains one or more lists as its elements.
students = [
["Rahul", 85],
["Priya", 92],
["Amit", 78]
]
print(students)
Output
[['Rahul', 85], ['Priya', 92], ['Amit', 78]]
You can use multiple indexes to access nested elements.
students = [
["Rahul", 85],
["Priya", 92],
["Amit", 78]
]
print(students[1][0])
print(students[1][1])
Output
Priya
92
marks = [78, 91, 85, 91, 66]
print("Highest:", max(marks))
print("Lowest:", min(marks))
marks.sort()
print("Sorted:", marks)
Output
Highest: 91
Lowest: 66
Sorted: [66, 78, 85, 91, 91]
products = ["Laptop", "Mouse", "Keyboard"]
products.append("Monitor")
products.sort()
print(products)
Output
['Keyboard', 'Laptop', 'Monitor', 'Mouse']
Consider the following program.
numbers = [40, 10, 30, 20]
numbers.sort()
print(numbers)
Execution Steps
sort() method is called.Output
[10, 20, 30, 40]
sort() when you want to permanently sort a list.copy() before modifying a list if the original data must be preserved.count() to analyze duplicate values.sort() with reverse()sort() arranges elements in order, while reverse() simply reverses the current order.
sort() Modifies the Original ListThe original list is changed after sorting.
index() for Missing ElementsIf the value is not found, a ValueError is raised.
copy() Creates a Deep CopyThe copy() method creates a shallow copy. Nested objects are still shared.
Remember that nested lists require multiple indexes, such as students[0][1].
In this section, you learned advanced Python list methods including sort(), reverse(), copy(), count(), and index(). You also explored nested lists, accessed nested elements, applied these concepts through practical examples, followed the execution flow of list methods, and reviewed best practices and common beginner mistakes. In the final section, you will review the complete lesson through a lesson summary, key takeaways, frequently asked questions, coding exercises, interview questions, a mini project, and a preview of the next lesson on Python List Comprehension: Complete Guide for Beginners.
In this lesson, you learned that Python lists are one of the most powerful and flexible data structures for storing multiple values in a single variable. Unlike variables that hold only one value, lists can store multiple items, maintain their order, allow duplicate values, and contain different data types.
You began by understanding what lists are, why they are important, and how to create them. You learned how to access list elements using positive and negative indexing and explored the characteristics that make lists different from other data structures.
Next, you learned how to slice lists, modify existing elements, add new items using append(), insert(), and extend(), and remove elements using remove(), pop(), del, and clear(). You also learned how to check whether an element exists in a list and how to determine the number of elements using the len() function.
Finally, you explored useful list methods such as sort(), reverse(), copy(), count(), and index(). You also learned about nested lists, practical real-world examples, best practices, and common mistakes beginners should avoid.
Lists are used extensively in data analysis, automation, web development, machine learning, and almost every Python application.
[]).0.-1.append(), insert(), and extend() add elements to a list.remove(), pop(), del, and clear() remove elements.sort() arranges elements in ascending or descending order.reverse() reverses the order of elements.copy() creates a shallow copy of a list.count() counts occurrences of an element.index() returns the index of the first matching element.A list is an ordered and mutable collection that stores multiple values in a single variable.
Yes. Python preserves the order in which elements are added.
Yes. Lists allow duplicate elements.
Yes. A single list can contain strings, integers, floats, Boolean values, and even other lists.
append() and insert()?append() adds an element at the end of the list, while insert() adds an element at a specific position.
remove() and pop()?remove() deletes an element by its value, whereas pop() removes an element by its index and returns it.
Use the len() function.
sort() method do?It arranges list elements in ascending order by default or descending order when reverse=True is used.
A nested list is a list that contains one or more lists as its elements.
Yes. Elements can be added, removed, or modified after the list is created.
append().insert().remove().pop().reverse() method.len().append() and extend().remove() and pop()?sort() method.copy() method work?Create a Python program that manages student records using lists.
Your program should:
append().========== STUDENT RECORDS ==========
Initial List
['Rahul', 'Priya', 'Amit']
After Adding
['Rahul', 'Priya', 'Amit', 'Neha']
After Removing
['Rahul', 'Amit', 'Neha']
Sorted List
['Amit', 'Neha', 'Rahul']
Total Students : 3
=====================================
Congratulations! You have successfully learned Python Lists. You now understand how to create lists, access elements, modify data, add and remove items, use slicing, work with built-in list methods, and organize information using nested lists.
In the next lesson, you will learn Python List Comprehension: Complete Guide for Beginners. You will discover how to create new lists efficiently using a concise syntax, apply conditions within list comprehensions, improve code readability, and write Python programs with fewer lines of code.