In Python, a list is a collection of items or values. It is like a container where you can store multiple things. These things can be anything — numbers, strings (words), or even other lists. Lists are very flexible because you can add, remove, or change items inside them.
A list is defined by placing items inside square brackets []
, separated by commas.
Example of a Simple List
my_list = [1, 2, 3, 4, 5]
Here, the list my_list
contains 5 numbers.
Accessing Items in a List
You can access items inside a list by using indexing. Python uses zero-based indexing, which means the first item in the list has index 0, the second item has index 1, and so on.
Example: Accessing Items
my_list = [1, 2, 3, 4, 5]
print(my_list[0]) # Output: 1
print(my_list[2]) # Output: 3
In the above example:
my_list[0]
gives1
(the first item).my_list[2]
gives3
(the third item).
Negative Indexing
You can also use negative numbers to access items from the end of the list.
-1
refers to the last item,-2
refers to the second-last item, and so on.
Example: Negative Indexing
my_list = [1, 2, 3, 4, 5]
print(my_list[-1]) # Output: 5
print(my_list[-2]) # Output: 4
Modifying Lists
You can change the values inside a list after it’s been created. To modify an item, you simply refer to the index and assign a new value.
Example: Changing Items
my_list = [1, 2, 3, 4, 5]
my_list[1] = 10 # Changing the second item (index 1) to 10
print(my_list) # Output: [1, 10, 3, 4, 5]
Adding Items to a List
Python lists have different ways to add items.
1. Using append()
The append()
method adds an item to the end of the list.
Example: Using append()
my_list = [1, 2, 3]
my_list.append(4) # Adding 4 to the end of the list
print(my_list) # Output: [1, 2, 3, 4]
2. Using insert()
The insert()
method allows you to add an item at a specific index in the list.
Example: Using insert()
my_list = [1, 2, 3]
my_list.insert(1, 5) # Inserting 5 at index 1
print(my_list) # Output: [1, 5, 2, 3]
3. Using extend()
The extend()
method is used to add multiple items at the end of a list.
Example: Using extend()
my_list = [1, 2, 3]
my_list.extend([4, 5, 6]) # Adding multiple items
print(my_list) # Output: [1, 2, 3, 4, 5, 6]
Removing Items from a List
You can also remove items from a list.
1. Using remove()
The remove()
method removes the first occurrence of a specified item.
Example: Using remove()
my_list = [1, 2, 3, 4, 5]
my_list.remove(3) # Removes the first occurrence of 3
print(my_list) # Output: [1, 2, 4, 5]
2. Using pop()
The pop()
method removes an item at a specified index and returns it.
Example: Using pop()
my_list = [1, 2, 3, 4, 5]
item = my_list.pop(2) # Removes and returns the item at index 2
print(item) # Output: 3
print(my_list) # Output: [1, 2, 4, 5]
If you don’t provide an index to pop()
, it will remove and return the last item.
List Operations
You can perform various operations on lists, such as checking if an item exists in the list or finding the length of the list.
1. Checking if an Item Exists
You can use the in
keyword to check if an item is in the list.
Example: Using in
my_list = [1, 2, 3, 4, 5]
print(3 in my_list) # Output: True
print(6 in my_list) # Output: False
2. Length of the List
You can use the len()
function to find the number of items in a list.
Example: Using len()
my_list = [1, 2, 3, 4, 5]
print(len(my_list)) # Output: 5
Slicing a List
You can get a portion (slice) of a list using slicing. The syntax for slicing is:
list[start:end]
start
is the index where the slice starts.end
is the index where the slice ends (but the item at theend
index is not included).
Example: Slicing
my_list = [1, 2, 3, 4, 5]
print(my_list[1:4]) # Output: [2, 3, 4]
If you omit start
or end
, Python will assume you want to go to the beginning or end of the list, respectively.
List Comprehensions
List comprehensions allow you to create new lists by applying an expression to each item in an existing list. It’s a compact way to create lists.
Example: List Comprehension
my_list = [1, 2, 3, 4, 5]
new_list = [x * 2 for x in my_list]
print(new_list) # Output: [2, 4, 6, 8, 10]
This doubles each item in the original list.