Python Lists
In Python, a list is an ordered collection of items. Lists can contain items of any data type, including numbers, strings, and even other lists.
Creating a List
Lists are created using square brackets [ ] and the items are separated by commas.
Accessing List items
You can access the items in a list using their indexes. The indexes start at 0 for the first item, 1 for the second item, and so on.
You can also use negative indexes to access items from the end of the list
This is an example:
Append item to a List
We can append items into a list using the following pattern .append(value)
method adds items to the tail of our list
Insert item in List
We can insert items into a list using the following pattern .insert(index, value)
. Here, you have to specify the index (i.e. the position) that you intend adding the item to.
Removing items from a list
There are variable numbers of ways to remove items from a list, we can remove items from the tail of our list using the .pop()
command, using the index of the item or even using the value of the item.
Removing the last item from the list using .pop
.pop
Removing items using the item index & the delete keyword del list[index]
del list[index]
Removing items using the value of the item list.remove(value)
list.remove(value)
Finding the index of an element in a list
In Python, a list index refers to the position of an element in a list. A list is an ordered collection of items, and each item has a corresponding index that indicates its position in the list.
Sorting a python list
Sorting is the process of rearranging the items in a list in a particular order. In Python, you can use the sorted()
function to sort a list. By default, the sorted() function sorts the items in ascending order.
We use the .sort()
and .sort(reverse=True)
to sort our python list.
Sorting items in ascending order:
Sorting items in descending order:
Slicing a list in python
In Python, you can use slicing to extract a portion of a list (also known as a sublist). Slicing uses a start index, a stop index, and an optional step size to specify the elements that should be included in the slice.
You can use slicing to modify the elements in a list by assigning new values to the slice. For example, you can use slicing to delete elements from a list by assigning an empty list to the slice:
Unpacking a list in python
In Python, you can use unpacking to assign the elements of a list to separate variables. This can be a convenient way to assign multiple variables at once, rather than assigning them one by one.
Example of list unpacking:
Looping through a python list
Looping is a programming construct that allows you to repeat a block of code a certain number of times or until a certain condition is met. In Python, there are two types of loops: for loops
and while loops
.
A for loop is used to iterate over a sequence (such as a list, tuple, or string) or an iterable object (such as a dictionary, file, or set). The for loop will execute a block of code for each element in the sequence, and it will automatically iterate through all the elements in the sequence.
You can use loops to perform a wide variety of tasks in Python, such as iterating over a list of items, searching for a particular element in a list, or repeating a set of actions until a certain condition is met. Loops are an essential part of programming and are used extensively in Python and other programming languages.
There are several ways to loop through a list in Python. Here are a few examples:
Using a for
loop:
Using a while
loop:
Using the .enumerate()
function:
Using a list comprehension:
Each of these techniques has its own use cases, and you can choose the one that is most appropriate for your needs. For example, you might use a for
loop if you want to perform a specific action for each element in the list, or a list comprehension if you want to create a new list based on the elements in the original list.
Python List Comprehension
List comprehension is a concise way to create a list using a single line of code. It is a powerful tool for creating lists based on existing lists or other iterable objects.
List comprehension has the following syntax: [expression for item in iterable]
List comprehension is a concise and efficient way to create lists in Python, and it is often used to create lists that are based on complex transformations or filters of other lists or iterable objects.
Last updated