Introduction
Learning how to handle lists is very important if you are new to Python programming. Python has many data types and lists falls under one of the most useful data types for holding groups of items. Before I introduce tips and practices that can help to make effective use of them, let’s focus on what these are.
List Indexing and Slicing
To explain the concept of python list, I’d advise you understand a list as being a collection of items. In lists, every item is always referred to by its position in the list which is always called index. This index assists you in adding or even deleting items on the list.
With this explanation,let’s say you have a list of fruits⬇️
With the image above, "apple" is at index [0], "banana" is at index [1], and so on.
Pls note: In Python, indexing starts from 0, not 1. So, if you want to get the first fruit, you’d do this ⬇️
Try this out on your dashboard.
Now lets talk about slicing
In Python, slicing is a technique of extracting portion of a given list, string or tuple by its range. Let us say you have a list of items that you do not want completely but part of it only. Slicing allows you to do that without breaking much sweat.
Try the example below on your dashboard ⬇️
Try this out on your dashboard too.
How about if you want to insert an item in the list,read here⬇️
Insert Items into a List
The insert() method is used whenever you need to add an item to a specific position on a list.
Here is an example ⬇️
fruits.insert(1, "blueberry")
print(fruits) # Output: ['apple', 'blueberry', 'banana', 'cherry', 'date', 'elderberry']
Remove Items from a List
To remove items from a list there’s a couple of options for you to consider. Its either you remove by value or remove by index.
To remove by value, Use the remove() method to remove the first occurrence of a value.
Let me break it down the remove by value method in the example here⬇️
fruits.remove("banana")
print(fruits) # Output: ['apple', 'blueberry', 'cherry', 'date', 'elderberry']
You see our output removed "banana" right?
Now to our second option, To Remove by index, use the pop() method to remove an item by its index.
Check this out ⬇️
fruits.pop(2)
print(fruits) # Output: ['apple', 'blueberry', 'date', 'elderberry']
You can see that "cherry" was removed with the pop() option
(refer to example output in remove by index for better context)
Sort Items in a List
The sort() method arranges the items in your list in ascending order by default. Check here⬇️
fruits.sort()
print(fruits) # Output: ['apple', 'blueberry', 'date', 'elderberry']
Now check the alphabetical arrangement above
Let me add a few tricks here,how about if you want to sort in descending order (not ascending)
If you want to sort in descending order, just pass reverse=True
Example here ⬇️
# Sorting the list in descending order
fruits.sort(reverse=True)
print(fruits) # Output: ['fig', 'elderberry', 'date', 'cherry', 'apple']
Now check the alphabetical arrangement above
Lets talk about list Comprehension.
List Comprehension
List comprehension is a simple way to create lists on a specific rule or condition.
For example, let’s create a list of squares of numbers from 0 to 9⬇️
You can also go ahead in adding conditions to filter items.
Let’s create a list of even squares below ⬇️
even_squares = [x**2 for x in range(10) if x % 2 == 0]
print(even_squares) # Output: [0, 4, 16, 36, 64]
There you have it! you’ve just learnt how to index and slice lists and use different methods to change them, as well as how to make new lists with list comprehensions.
These methods are really awesome and you are going to find yourself coding much easier than before with them and habits will grow the more these techniques are repeated and practicalized, in fact, that is what our practical course here offers you. Start practicing here now.