Understanding a Python list and useful list functions

By Abhinash on July 26, 2022

Among the different types of sequence data used in Python lists are the most versatile and hence a frequently used data type. Lists are ordered collections of heterogeneous Python objects such as integer, string, Boolean or other lists. In Python, the collection of objects is enclosed between square brackets [ ] separated by a comma.

Lists can be created by putting all objects within the square brackets and assigning them to a variable name.

cake = [‘flour’, ‘eggs’, ‘sugar’, ‘butter’, ‘baking powder’, ‘baking soda’, ‘vanilla essence’]

Here, the ingredients of the cake are elements and are placed within [ ]. The elements are assigned to the variable, ‘cake’.

Sam = [545, ‘South Lake Union’, ‘Stewart Street’, ‘Seattle’, ‘WA’, ‘98109’]

The above list contains string and integer data, each element separated by a comma.

Creating a list of variables

Park_Avenue = 545
Hill_View = 47
North_Hill = 672
area = [Park_Avenue, Hill_View, North_Hill]
print(area)
#OUTPUT
[545,47,672]

Lists can also be created by combining several variables as shown above.

Here, ‘Park_Avenue’, ‘Hill_View’ and ‘North_Hill’ have been defined as variables. By combining these variables into a new list, ‘area’.

Subsets of lists or nested lists

Moreover, lists in Python can also consist of sub-lists as elements, and such lists are referred to as nested lists.

to_do = [‘laundry’, ‘cooking’, ‘report writing’, [‘milk’, ‘eggs’, ‘bread’, ‘butter’], ‘gardening’]

In this list, there is a sub-list, [‘milk’, ‘eggs’, ‘bread’, ‘butter’] representing the shopping list items, which means that the main list, ‘to_do’ is a nested list.

Determining the length of a list by len()

The length of a string, sequence or object can be determined by using the len() function. This same function can be used to determine the total number of elements in a list.

cake = [‘flour’, ‘eggs’, ‘sugar’, ‘butter’, ‘baking powder’, ‘baking soda’, ‘vanilla essence’]
print(len(cake))
#OUTPUT
7

Accessing a specific element by its index

To print or access a specific element of a list, using the element’s position as the index.

cake = [‘flour’, ‘eggs’, ‘sugar’, ‘butter’, ‘baking powder’, ‘baking soda’, ‘vanilla essence’]
print(cake[2])
#OUTPUT
sugar

The indexing of elements in a Python list starts from 0. So the first element of the list can be accessed with index ‘0’ and so on.

Elements of sub-lists of a nested list can also be accessed by their index.

to_do = [‘laundry’, ‘cooking’, ‘report writing’, [‘milk’, ‘eggs’, ‘bread’, ‘butter’], ‘gardening’]
print(to_do[3][1])
#OUTPUT
eggs
TIP

A specific element of a list can be manipulated by accessing the element by its index.

Slicing a Python list by its index

A list of any type can be sliced by its index as a reference. To slice the list first specify the starting index and then the end index.

cake = [‘flour’, ‘eggs’, ‘sugar’, ‘butter’, ‘baking powder’, ‘baking soda’, ‘vanilla essence’]
print(cake[1:4])
#OUTPUT
['eggs',‘sugar’, ‘butter’, ‘baking powder’]
TIP

This is how you can search the index of an element in a list.

print(cake.index(‘eggs’))
#OUTPUT
1

Counting the occurrence of an element in a list

Use the count function to count the number of times an element appears in a list.

squares = [4, 9, 16, 4, 4, 49, 81, 4]
print(squares.count(4))
#OUTPUT
2

Discuss