Lists in Python
We were introduced to the list data type in an earlier blog, and in this blog, we will explore the different ways to use a list.
Consider the following example:
list_variable = ["Python", "Dynamically Typed", "Programming Language"]
Accessing items in a List
To select an item from the list you simply add the index number of the item within square brackets, after the variable name. Thus, to print the first element, we need to do the following:
print(list_variable[0]) # Prints "Python"
In a scenario where we do not know the number of items in a list and supposing we want to access the last item of the list we use [-1] and in a like manner, if use -2 we can access the second last item.
print(list_variable[-1]) # Prints "Programming Language"
We can view a select number of items from a list, by doing the following.
print(list_variable[1:3])
NOTE: Even though the index value ends at 2 (because there are 3 items), we use 3 because Python stops at “n-1” if “n” is the endpoint. Thus over here when we give 3 as the endpoint, Python will stop at index 2.
Add items to a List
To add items to a list we can use the append() function which allows us to add items to the end of a list.
list_variable = ["Python", "Dynamically Typed", "Programming Language"] print("Before adding") print(list_variable) list_variable.append("Blogs") # Adding "Blogs" print("\nAfter adding") print(list_variable)
Adding items at a specific position to a List
We can also add items at a specific location in the list using the insert() function. The first argument we give is the index position and then we add the item we want to enter into the list.
list_variable = ["Python", "Dynamically Typed", "Programming Language"] print("Before adding") print(list_variable) list_variable.insert(1, "Technical Writing") # Adding "Technical Writing" at index 1 print("\nAfter adding") print(list_variable)
Removing items from a List
To remove a specified value from the list, we can use the remove() function which removes the first occurrence of the value.
list_variable = ["Python", "Dynamically Typed", "Programming Language", "Python"] print("Before removing") print(list_variable) list_variable.remove("Python") # Removing the first occurrence of Python print("\nAfter removing") print(list_variable)
Sorting items in a List
The sort() function allows us to sort a list in order, whether the items be alphabets or numbers, or strings.
list_variable = ["Python", "Dynamically Typed", "Programming Language"] print("Before sorting") print(list_variable) list_variable.sort() print("\nAfter sorting") print(list_variable)
Emptying a List
We can clear out a list of items by using the clear() function. This removes all of the items stored within a list.
list_variable = ["Python", "Dynamically Typed", "Programming Language"] print("Before clearing") print(list_variable) list_variable.clear() print("\nAfter clearing") print(list_variable)
Accessing all items in a List
To individually access each item in a list, we need to use a loop. We haven’t discussed loops yet, but as I said before the for loop is one of the looping techniques available in Python, which we can use to access every loop in a value.
list_variable = ["Python", "Dynamically Typed", "Programming Language"] for value in list_variable: print(item)
The term ‘value’ is a variable that is automatically created by the for loop to store the value that is held in each iteration of the loop. Don’t worry we will cover loops in detail in a separate blog, but for now, keep in mind that ‘value’ will hold each item of the list one at a time and that is why we will get the following output:
What have we learned?
.How to access an item within a list?
.How to select the last item of a list?
.How can we add items to a list at a particular index?
.How to remove an item from a list?
.How do we empty a list?
.How can we sort a list in order?
.How can we access each item in a list?