In Python programming, a list is a collection of ordered elements enclosed in square brackets
. Lists can hold any data type such as numbers, strings, booleans, and even other lists. Lists are one of the most frequently used data structures in Python.
This lesson will cover the following topics:
- Creating Lists
- Indexing and Slicing Lists
- Adding and Removing Elements from Lists
- Sorting Lists
- Looping Through Lists
To create a list in Python, you enclose the list elements with square brackets
 separated by commas. Here is an example of a list of integers:
my_list = [1, 2, 3, 4, 5]
You can also create a list of strings:
my_string_list = ["apple", "banana", "cherry"]
Or even a mixed list:
mixed_list = [1, "apple", True, 3.14]
Indexing and Slicing Lists
Indexing and slicing are methods used to access specific elements of a list. Indexing allows you to access the value of an element based on its position in the list. The index starts at 0 for the first element and increases by one for each subsequent element.
Here is an example of indexing a list to retrieve the first element:
my_list = [1, 2, 3, 4, 5] first_element = my_list print(first_element)
Slicing a list extracts a range of elements from the list. The syntax for slicing is
list[start:stop], where the start index is inclusive and the stop index is exclusive.
Here is an example of slicing a list to retrieve the first three elements:
my_list = [1, 2, 3, 4, 5] first_three_elements = my_list[0:3] print(first_three_elements)
[1, 2, 3]
You can also use negative indexing to access elements from the end of the list. For example, to access the last element in the list, you can use
-1 as the index:
my_list = [1, 2, 3, 4, 5] last_element = my_list[-1] print(last_element)
Adding and Removing Elements from Lists
Lists are mutable, which means that you can add or remove elements from them after they have been created.
To add an element to the end of a list, you can use the
my_list = [1, 2, 3, 4, 5] my_list.append(6) print(my_list)
[1, 2, 3, 4, 5, 6]
To insert an element at a specific position in a list, you can use the
my_list = [1, 2, 3, 4, 5] my_list.insert(3, 3.5) print(my_list)
[1, 2, 3, 3.5, 4, 5]
To remove an element from a list, you can use the
my_list = [1, 2, 3, 4, 5] my_list.remove(3) print(my_list)
[1, 2, 4, 5]
You can sort a list in Python using the
sort() method. By default, the
sort() method sorts the list in ascending order.
Here is an example of sorting a list of numbers:
my_numbers = [4, 1, 3, 2, 5] my_numbers.sort() print(my_numbers)
[1, 2, 3, 4, 5]
You can also sort a list in descending order by passing the argument
reverse=True to the
my_numbers = [4, 1, 3, 2, 5] my_numbers.sort(reverse=True) print(my_numbers)
[5, 4, 3, 2, 1]
Looping Through Lists
Looping through a list is a common operation in Python. You can use `for` loops to iterate over the elements of a list. Here is an example of looping through a list of strings:
fruits = ["apple", "banana", "cherry"] for fruit in fruits: print(fruit)
apple banana cherry
You can also loop through a list using its indices with the
fruits = ["apple", "banana", "cherry"] for i in range(len(fruits)): print(fruits[i])
apple banana cherry
Now that you have learned the basics of lists in Python, it's time to practice! Here are some exercises to help you solidify your understanding:
- Create a list of your favorite colors and print its second element.
- Create a list of numbers from 1 to 10 and slice it to get the odd numbers.
- Add the number 7 to the list of numbers from 1 to 10 and then sort the list in descending order.
- Loop through the list of your favorite colors and print each color in uppercase.
Lists are an essential part of programming in Python. They allow you to store collections of data and manipulate them in various ways. In this lesson, you have learned how to create lists, access their elements using indexing and slicing, add and remove elements from them, sort them, and iterate through them using loops. Keep practicing these concepts, and you will be well on your way to mastering Python lists!//= htmlentities($post["body"]); ?>