|
| Home |
|---|
A Python list is one of the most commonly used data structures in Python. A list allows you to store multiple values in a single variable and work with a collection of related data.
Python lists are ordered, changeable and allow duplicate values. They can contain numbers, strings, Boolean values, objects and even other lists. Lists are used extensively in beginner Python programs as well as data analysis, automation, machine learning, web development and many other applications.
Learning how to work with lists is an essential part of learning Python. Once you understand lists, you can use them to store data, process collections of values, perform calculations, search for information, filter data and build more complex programs.
A Python list is an ordered collection of items.
Lists are written using square brackets [], with individual
elements separated by commas.
For example, the following list stores the names of four programming languages:
languages = ["Python", "Java", "C++", "JavaScript"]
print(languages)
['Python', 'Java', 'C++', 'JavaScript']
Each value stored in the list is called an element or
list item. The list itself is assigned to the variable
languages.
Lists are particularly useful when a program needs to store multiple related values and process those values together.
The basic syntax for creating a list is:
list_name = [item1, item2, item3]
For example:
numbers = [10, 20, 30, 40, 50]
Items are separated by commas, and the complete collection is enclosed in square brackets.
A list can contain any number of elements, including no elements at all. An empty list can be created using a pair of square brackets:
numbers = []
The simplest way to create a Python list is to place values inside square brackets.
fruits = ["Apple", "Banana", "Orange", "Mango"]
print(fruits)
['Apple', 'Banana', 'Orange', 'Mango']
Lists can also be created using the list() constructor.
This is useful when you want to convert another iterable, such as a
tuple, string or range, into a list.
numbers = list((10, 20, 30, 40))
print(numbers)
[10, 20, 30, 40]
Python lists are ordered collections. Each element has a position within the list, and that order is normally preserved unless the list is deliberately changed.
colors = ["Red", "Green", "Blue"]
print(colors)
The order of elements is important because Python assigns an index to each position in the list. These indexes can then be used to work with individual elements.
Python uses zero-based indexing. This means that the
first item in a list has index 0, the second item has
index 1, and the third item has index 2.
fruits = ["Apple", "Banana", "Orange"]
print(fruits[0])
print(fruits[1])
print(fruits[2])
Apple
Banana
Orange
Python also supports negative indexing. Negative indexes count from
the end of the list, with -1 representing the last item.
fruits = ["Apple", "Banana", "Orange"]
print(fruits[-1])
Orange
Indexing is one of the fundamental concepts behind working with Python lists. It is used when accessing and changing individual elements and also plays an important role in list slicing.
A Python list can contain the same value more than once. Unlike a set, a list does not require every element to be unique.
numbers = [10, 20, 20, 30, 30]
print(numbers)
[10, 20, 20, 30, 30]
Duplicate values can be useful when the occurrence of each value is meaningful, such as when storing scores, transactions, names or other sequential data.
Python lists can contain values of different data types. A single list can contain integers, strings, floating-point numbers, Boolean values and other Python objects.
data = [10, "Python", 3.14, True]
print(data)
[10, 'Python', 3.14, True]
Although Python allows mixed data types, lists in many practical programs contain values of the same or related types because this makes processing the data easier.
One of the most important characteristics of a Python list is that it is mutable. This means that the contents of a list can be changed after the list has been created.
Existing elements can be replaced, new elements can be added and existing elements can be removed.
numbers = [10, 20, 30]
numbers[1] = 200
print(numbers)
[10, 200, 30]
List mutability makes lists particularly useful when a program needs to update a collection of data while it is running.
The built-in len() function returns the number of elements
contained in a list.
fruits = ["Apple", "Banana", "Orange", "Mango"]
print(len(fruits))
4
The length of a list is frequently used when processing data, performing calculations and determining the number of elements that need to be processed.
List indexes allow individual elements to be accessed by their position. Python supports both positive and negative indexing, making it possible to access elements from either the beginning or the end of a list.
Accessing list elements is one of the first practical operations you will perform with a Python list. It is also an important foundation for changing values, using slices and processing lists with loops.
Because lists are mutable, existing elements can be changed after the list has been created. An individual item can be replaced, and a range of elements can also be changed.
Changing list items is useful when data needs to be corrected, updated or transformed during program execution.
New elements can be added to a Python list in several ways. Python provides methods for adding an item at the end of a list, inserting an item at a particular position and adding multiple elements.
Adding list items is a common operation when building collections dynamically as a program runs.
Python provides several techniques for removing elements from a list. Depending on the situation, you may remove an item by its value, by its index or remove all elements from the list.
Choosing the appropriate removal technique depends on whether you know the value or position of the item and whether the original list needs to be retained.
Lists are often processed one element at a time. Python's
for loop provides a simple and readable way to iterate
through the elements of a list.
fruits = ["Apple", "Banana", "Orange"]
for fruit in fruits:
print(fruit)
Apple
Banana
Orange
List iteration is useful for displaying values, performing calculations, checking conditions, filtering data and applying operations to each element.
List slicing allows you to extract a portion of a list rather than working with only one element. A slice can specify a starting position, stopping position and optional step.
The general syntax is:
list[start:stop:step]
Slicing is useful for creating sublists, selecting ranges of values, skipping elements and working with portions of a larger list.
List concatenation means combining two or more lists to create a larger collection.
first = [1, 2, 3]
second = [4, 5, 6]
combined = first + second
print(combined)
[1, 2, 3, 4, 5, 6]
Python provides more than one way to combine lists. The appropriate approach depends on whether you want to create a new list or modify an existing list.
A nested list is a list that contains one or more other lists. Nested lists can be useful for representing structured data such as rows, columns, tables and simple matrices.
students = [
["Ali", 18],
["Sara", 19],
["John", 20]
]
print(students)
Nested lists introduce another level of structure to list data. Accessing their contents involves working with multiple indexes.
A list comprehension provides a concise way to create a new list from an existing iterable. List comprehensions are commonly used to transform or filter data.
The basic structure of a list comprehension is:
[expression for item in iterable]
Conditions can also be included when only certain elements should be included in the resulting list.
numbers = [1, 2, 3, 4, 5]
squares = [number ** 2 for number in numbers]
print(squares)
[1, 4, 9, 16, 25]
List comprehensions are especially useful for simple transformations and filtering operations where a traditional loop would require more code.
Python provides a collection of built-in list methods that make it easier to manipulate and work with list data.
List methods can be used to add and remove elements, search for values, count occurrences, sort data, reverse a list and create a copy of an existing list.
Important Python list methods include append(),
insert(), extend(), remove(),
pop(), clear(), index(),
count(), sort(), reverse()
and copy().
The individual methods are organised in the Python List Methods section of the sidebar, where each method can be studied separately with its own examples and explanation.
Lists are often used together with other Python data structures and data types. Python provides several ways to convert lists into other structures and to create lists from other types of data.
Common examples include converting a list to a string, converting a string to a list, converting a list to a set, converting a tuple to a list and creating a dictionary from list data.
The individual conversion tutorials are organised under List Conversions in the sidebar.
Lists are frequently used in programming exercises and real-world applications where collections of data need to be searched, analysed, filtered or transformed.
Common list problems include finding the largest or smallest value, calculating a total or average, searching for an element, removing duplicate values, filtering elements and combining collections.
These problems often require a combination of Python lists, loops, conditions, built-in functions and list comprehensions.
numbers = [10, 25, 5, 40, 15]
largest = max(numbers)
smallest = min(numbers)
total = sum(numbers)
print(largest)
print(smallest)
print(total)
40
5
95
Learning to solve these types of problems helps you move beyond memorising list syntax and develop practical Python programming skills.
Beginners often encounter errors when working with list indexes, modifying list elements or using an inappropriate data type. Understanding the cause of these errors makes list-based programs much easier to debug.
An IndexError occurs when a program attempts to access a
list position that does not exist.
numbers = [10, 20, 30]
print(numbers[5])
The valid indexes in this example are 0, 1
and 2. Index 5 is outside the available range.
A TypeError can occur when an inappropriate type is used
as a list index.
numbers = [10, 20, 30]
print(numbers["one"])
List indexes normally need to be integers, such as
numbers[0].
Another common mistake is attempting to assign a value to an index that does not yet exist.
For example, if a list contains three elements, assigning directly to
index 5 will result in an error. When a new item needs to
be added, an appropriate list operation should be used instead.
Understanding indexing, mutability and the difference between changing an existing element and adding a new element can prevent many common list errors.
Python lists are useful far beyond simple programming exercises. They are commonly used whenever a program needs to work with a collection of values.
students = ["Ali", "Sara", "John", "Ayesha"]
print(students)
scores = [85, 92, 78, 90, 88]
print(scores)
scores = [85, 92, 78, 90, 88]
average = sum(scores) / len(scores)
print(average)
Lists are also commonly encountered when working with files, APIs, databases, data analysis libraries and other Python applications. Understanding lists therefore provides a foundation for many areas of Python programming.
Python provides several built-in data structures for storing and organising information. Lists are particularly useful when you need an ordered and changeable collection that can contain duplicate values.
| Data Structure | Main Characteristics |
|---|---|
| List | Ordered, changeable and allows duplicate values. |
| Tuple | Ordered collection generally used when the values should not be changed. |
| Set | Collection designed for unique values. |
| Dictionary | Stores data as key-value pairs. |
Choosing the appropriate data structure depends on what your program needs to do with the data. Lists are often the natural starting point when order and mutability are important.
Python lists provide a flexible way to store and process collections of values. They are ordered, mutable and allow duplicate elements, making them suitable for a wide range of programming tasks.
The fundamentals begin with creating a list, understanding indexes, checking its length and recognising that lists can be changed after they are created.
From there, Python lists can be accessed, modified, expanded and reduced. More advanced techniques include slicing, concatenation, nested lists and list comprehensions.
Python's built-in list methods provide convenient operations for manipulating list data, while list conversions allow lists and other Python data structures to be transformed when required.
Lists are also an important foundation for solving practical Python programming problems involving searching, calculations, filtering, sorting and data processing.
Use the Python Lists sections in the sidebar to move from these fundamentals to detailed tutorials on individual list operations, methods and conversions.
Struggling with Python syntax, assignments or debugging errors? Learning alone can be frustrating.
Get 1-on-1 Python tutoring and learn faster with clear explanations, real examples and live help.
Home Tutorial Online Live Trainings Contact Us © 2026 All rights reserved by www.PythonTutor.net