In this python tutorial, you will learn:

What is a Python List?
Examples of Python lists:
Accessing values within lists
Python List slicing
Updating lists
Deleting list elements
Appending list elements
List built-in functions (methods)
Looping through lists
List Comprehensions Python

Examples of Python lists:

Python lists can be homogeneous, meaning that they can contain the same type of objects; or heterogeneous, including different types of objects. Examples of homogeneous lists include: Examples of heterogeneous lists include:

Accessing values within lists

To access values within lists, the index of the objects inside the lists can be used. An index in Python lists refers to the position of an element within an ordered list. For example:

The first value in the list above, 3, has an index of 0 The second value, 22, has an index of 1 The third value, 30, has an index of 2

and so on. To access each of the values from the list, you would use: The last member of a list can also be accessed by using the index -1. For example,

Python List slicing

List slicing is the method of splitting a subset of a list, and the indices of the list objects are also used for this. For example, using the same list example above; Python lists are upper-bound exclusive, and this means that the last index during list slicing is usually ignored. That is why , and not [30, 5.3, 20]. The same goes for all the other list slicing examples given above.

Updating lists

Let’s say you have a list = [physics, chemistry, mathematics], and you want to change the list to [biology, chemistry, mathematics], effectively changing the member at index 0. That can easily be done by assigning that index to the new member you want. That is, Output: [biology, chemistry, mathematics] This replaces the member at index 0 (physics) with the new value you want (chemistry). This can be done for any member or subset of the list you want to change. To give another example; let’s say you have a list called integers and containing the numbers [2, 5, 9, 20, 27]. To replace 5 in that list with 10, you can do that with: To replace the last member of the list of integers, which is 27, with a free number like 30.5, you would use:

Deleting list elements

There are 3 Python methods for deleting list elements: list.remove(), list.pop(), and del operator. Remove method takes the particular element to be removed as an argument while pop and del take the index of the element to be removed as an argument. For example: list = [3, 5, 7, 8, 9, 20] To delete 3 (the 1st element) from the list, you could use:

list.remove(3) or
list.pop[0], or
del list[0]

To delete 8, the item at index 3, from the list, you could use:

list.remove(8), or
list.pop[3]

Appending list elements

To append elements to a list, the append method is used, and this adds the element to the end of the list. For example:

List built-in functions (methods)

The following is a list of list built-in functions and methods with their descriptions:

len(list): this gives the length of the list as output. For example:

max(list): returns the item in the list with the maximum value. For example:

min(list): returns the item in the list with the minimum value. For example:

list(tuple): converts a tuple object a list. For example;

list.append(element): appends the element to the list. For example;

list.pop(index): removes the element at the specified index from the list. For example;

list.remove(element):deletes the element from the list.For example;

list.reverse(): reverses the objects of the list. For example;

list.index(element): to get the index value of an element within the list. For example;

sum(list): to get the sum of all the values in the list, if the values are all numbers (integers or decimals). For example;

If the list contains any element that is not a number, such as a string, the sum method would not work. You would get an error saying: “TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’”

list.sort(): to arrange a list of integers, floating point numbers, or strings, in ascending or descending order. For example:

Another example: A list of strings can also be sorted, either alphabetically, or by length of the strings. For example; We can sort the same list alphabetically by using ‘strings.

Looping through lists

Looping through lists can be done in just the same way as any other looping function in Python. This way, a method can be performed on multiple elements of a list at the same time. For example: list = [10, 20, 30, 40, 50, 60, 70]. To loop through all the elements of this list, and let’s say, add 10 to each element: To loop through the first three elements of the list, and delete all of them; To loop through the 3rd (index 2) to last element on the list, and append them to a new list called new_list: In this way, any or method or function can be applied to the members of a list to perform a particular operation. You can either loop through all the members of the list, or loop through a subset of the list by using list slicing.

List Comprehensions Python

List comprehensions are Python functions that are used for creating new sequences (such as lists, dictionaries, etc.) using sequences that have already been created. They help to reduce longer loops and make your code easier to read and maintain. For example; let’s say you wanted to create a list which contains the squares of all the numbers from 1 to 9: To do the same thing with list comprehensions: As seen above, writing the code using list comprehensions is much shorter than using traditional for loops, and is also faster. This is just one example of using list comprehensions in place of for loops, but this can be replicated and used in a lot of places where for loops can also be used. Sometimes, going with a for loop is the better option, especially if the code is complex, but in many cases, list comprehensions will make your coding easier and faster. Below is a table containing some list functions and methods, and their descriptions.

Built-in Functions

List Methods

Summary

A list is exactly what it sounds like, a container that contains different Python objects, which could be integers, words, values, etc. Python lists can be homogeneous, means they can contain the same type of objects; or heterogeneous, containing different types of objects. To access values within lists, the index of the objects inside the lists can be used. List slicing is the method of splitting a subset of a list, and the indices of the list objects are also used for this. Three methods for deleting list elements are : 1)list.remove(), 2)list.pop(), and 3)del operator Append method is used to append elements. This adds the element to the end of the list. Looping method of Python program can be performed on multiple elements of a data list at the same time. List comprehensions are Python functions that are used for creating new sequences (such as lists, dictionaries, etc.) using sequences that have already been created.