Python 3 – Lists
In Python, a list is used to store a collection of items, such as names, numbers, or even other lists. Lists are ordered, changeable, and allow an item to appear more than once.
Creating a list
We can create a list by passing a series of comma-separated values, enclosed in square brackets, to the list()
constructor, or by simply enclosing the values in square brackets. For example:
my_list = list(("apple", "banana", "cherry")) # using the list() constructor
my_list = ["apple", "banana", "cherry"] # using square brackets
In the examples above, we have created a list containing three items, which are strings.
Accessing items in a list
We can access individual items in a list by referring to their index number, which starts at 0 for the first item. For example, to access the first item in the list we created above, we would use the following code:
print(my_list[0]) # output: "apple"
We can also use negative indexing, which starts at -1 for the last item in the list, to access items from the end of the list. For example, to access the last item in the list, we would use the following code:
print(my_list[-1]) # output: "cherry"
Modifying items in a list
We can modify individual items in a list by referring to their index number and assigning a new value. For example, to change the second item in the list we created above to “orange”, we would use the following code:
my_list[1] = "orange"
print(my_list) # output: ["apple", "orange", "cherry"]
Adding items to a list
We can add items to a list by using the append()
method to add an item to the end of the list, or the insert()
method to insert an item at a specific index. For example, to add “lemon” to the end of the list we created above, we would use the following code:
my_list.append("lemon")
print(my_list) # output: ["apple", "orange", "cherry", "lemon"]
To insert “grape” at the second position in the list, we would use the following code:
my_list.insert(1, "grape")
print(my_list) # output: ["apple", "grape", "orange", "cherry", "lemon"]
Removing items from a list
We can remove items from a list by using the remove()
method to remove a specific item or the pop()
method to remove an item at a specific index. For example, to remove “orange” from the list we created above, we would use the following code:
my_list.remove("orange")
print(my_list) # output: ["apple", "grape", "cherry", "lemon"]
To remove the item at the third position in the list, we would use the following code:
my_list.pop(2)
print(my_list) # output: ["apple", "grape", "lemon"]
Looping through a list
We can loop through the items in a list using a for
loop. For example, to print each item in the list we created above, we would use the following code:
for x in my_list:
print(x)
Checking if an item exists in a list
We can check if a specific item exists in a list by using the in
keyword. For example, to check if “grape” is in the list we created above, we would use the following code:
if "grape" in my_list:
print("Yes, 'grape' is in the list")
List length
We can get the number of items in a list by using the len()
function. For example, to get the length of the list we created above, we would use the following code:
print(len(my_list)) # output: 3
List methods
There are many other methods that can be used with lists in Python, including:
sort()
: sorts the items in a listreverse()
: reverses the order of the items in a listcopy()
: returns a copy of a list
For example, to sort the list we created above in alphabetical order, we would use the following code:
my_list.sort()
print(my_list) # output: ["apple", "grape", "lemon"]
Conclusion
In this article, we have discussed what lists are in Python, how to create and modify them, and how to use various methods to work with lists. Lists are a fundamental data structure in Python and are used extensively in many different types of applications. By understanding how to work with lists, you will be able to write more effective and efficient Python code.