How to Find the Length of the List in Python

by:

Data AnalysisPython

This short article shows how to find the length of list in Python. The same function can be used to get the number of elements in a set, dict, or tuple in Python.

1. How to Find Length of List in Python

In Python, you can use the function len() to find the size of a list – please see the example below.

# create list of numbers
>>> list_numbers = [1, 2, 3, 4, 5, 6, 7]

# check length of list
>>> len(list_numbers)

# returns list length
>>> 7

The same function could be used to find the number of elements in a dictionary, set, or tuple.

#### SET
# create set of numbers
>>> set_numbers = {1,2,3}

# check length of set
>>> len(set_numbers)

# returns set length
>>> 3

#### DICT
# create dict of city names
>>> dict_cities = {"Chicago":"USA", "Tokyo": "Japan"}

# check length of dict
>>> len(dict_cities)

# returns set length
>>> 2

#### TUPLE
# create tuple of city names
>>> tuple_numbers = ((1,2))

# check length of dict
>>> len(tuple_numbers)

# returns set length
>>> 1

More Resources

Here are three of my favorite Python Books in case you want to learn more about it.

Related Posts