A list in Python is a built-in data structure used to store multiple values
in a single variable. Lists are flexible, easy to use, and widely applied
when working with collections of related data.
Creating a List
Lists are created using square brackets
[], with
items separated by commas.
What Is a List?
In Python, a list is one of the four core collection data types:
List – Ordered, changeable, allows duplicate values
Dictionary – Ordered, changeable, stores data as
key–value pairs
Each collection type is designed for a different use case.
List Characteristics
1. Ordered
Lists maintain a fixed order. Items appear in the same sequence in which
they were added. New elements are always added at the end unless specified
otherwise.
2. Changeable (Mutable)
Lists can be modified after creation—you can add, update, or remove
elements.
3. Allows Duplicate Values
Lists can store the same value multiple times because each item has a unique
index.
Indexing in Lists
Each list item has a position called an index, starting from
0.
Finding the Length of a List
Use the
len()
function to determine how many elements a list contains.
List Items and Data Types
A list can store elements of any data type.
Checking the Data Type of a List
From Python’s perspective, lists are objects of type
list.
Creating a List Using the list() Constructor
You can also create a list using the
list()
constructor, especially when converting other iterable data types.
Note: Double parentheses are used because the inner one
defines a tuple.
Python – Accessing List Items
In Python, list items are stored in an indexed sequence, which allows you to
access specific elements by referring to their position. Indexing makes it
easy to retrieve individual items or slices of data from a list.
Accessing Items by Index
Explanation: Index 1 refers to the second element in the
list. Python uses zero-based indexing.
Negative Indexing
Negative indexing allows access to list items from the end.
-1 refers to
the last item,
-2 refers to
the second last item, etc.
Accessing a Range of Items
You can retrieve multiple items at once using slicing.
Explanation: Starts from index 2 (included) and ends at
index 5 (excluded).
Omitting the Start Index
Omitting the End Index
Range of Negative Indexes
Checking if an Item Exists in a List
Use the
in keyword to
check whether a specific value exists in a list. Commonly used in validation
and conditional logic.
Python – Changing List Items
Python lists are mutable, which means their contents can be modified after
creation. You can update individual elements, replace multiple values at
once, or insert new items at a specific position.
Changing a Single Item
Changing Multiple Items Using a Range
Replacing with More/Fewer Items Than Removed
If you insert more elements than you replace, the list size
increases:
If you insert fewer elements than you remove, the list size
decreases:
Inserting Items Without Replacing Existing Ones
To add a new item at a specific position without removing any elements, use
the
insert()
method. Existing elements shift to the right.
Python – Adding Items to a List
Adding an Item Using append()
Inserting an Item at a Specific Position
Extending a List with Another Iterable
The
extend()
method allows you to add multiple items at once by appending elements from
another list, tuple, set, or dictionary.
Example: Adding a Tuple to a List
Method
Adds
Position
append()
One item
End of list
insert()
One item
Specific index
extend()
Multiple items
End of list
Python – Removing List Items
Removing an Item by Value
The
remove()
method deletes the first occurrence of a specified value. If the list
contains duplicates, it deletes only the first match.
Removing an Item by Index Using pop()
The
pop() method
removes an element based on its index. If no index is specified, it removes
the last element.
Removing an Item Using del
The
del keyword
can remove an element at a specific index or delete the entire list object.
Clearing All Items from a List
Python – Looping Through a List
Looping Through List Items Using a for Loop
Looping Using Index Numbers
Looping Through a List Using a while Loop
Looping with List Comprehension
List Comprehension in Python
List comprehension is a concise and readable way to create a new list from
an existing iterable. It reduces multiple lines of code into a single,
expressive statement.
General Syntax:
Traditional Approach (Without List Comprehension)
Using List Comprehension (Recommended)
Using Conditions as Filters
Without Any Condition
Working with Different Iterables
Modifying Values Using Expressions
Conditional Expressions Inside List Comprehension
Case-Insensitive Sorting in Python Lists
Case-Insensitive Sorting Using a Key Function
Reversing a List
Tip: Sort and reverse at the same time:
Copying a List in Python
In Python, assigning one list to another using
= does not
create a new list. Both variables point to the same list object.