Obtaining the list length

Finding the number of the elements in a list is easy – you simple need to call the len() function and pass it the list as the argument:

last_names = ['Jones', 'Antunovich', 'Daniels', 'Thomson']
print(len(last_names))

>>>
4
>>>

You can use the result of the len() function to get the value of the last element of a list. Example:

last_element = len(last_names) - 1
print(last_names[last_element])

>>>
Thomson
>>>

Because list indices start from 0, we had to subtract 1 from the length of the list.

Geek University 2022