×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Python - List Length and Count

Last Updated : May 01, 2025

In Python, you can determine the size of a list using the len() function and count specific elements using the count() method. In this chapter, we will learn how to find the length of a list, count occurrences of elements, and understand their usage through practical examples.

Getting Length of a List Using len()

The len() function is used to get the number of elements in a list.

Example

In this example, we are getting the total number of cities in a list using the len() function.

# Creating a list of cities
cities = ["New York", "London", "Paris", "Tokyo", "Sydney"]

# Getting the length of the list
length = len(cities)

# Displaying the length
print("Number of cities:", length)

The output of the above code would be:

Number of cities: 5

Counting Occurrences Using count()

The count() method returns the number of times a specified value appears in the list.

Example

In this example, we are counting how many times 'Paris' appears in the list of cities.

# Creating a list of cities with duplicate values
cities = ["New York", "Paris", "London", "Paris", "Tokyo", "Paris"]

# Counting the number of times 'Paris' appears
count_paris = cities.count("Paris")

# Displaying the count
print("'Paris' appears", count_paris, "times.")

The output of the above code would be:

'Paris' appears 3 times.

Using len() on Nested Lists

When using len() on a nested list, it returns the number of top-level elements (i.e., the outer list's length), not the total count of all inner elements.

Example

In this example, we are calculating the number of regions (sublists) in a nested city list.

# Creating a nested list of cities
cities = [
    ["New York", "Los Angeles"],
    ["London", "Manchester"],
    ["Tokyo", "Osaka"]
]

# Getting the number of regions
num_regions = len(cities)

# Displaying the number of regions
print("Number of regions:", num_regions)

The output of the above code would be:

Number of regions: 3

Counting All Items in a Nested List

To get the total number of all items in a nested list, you need to loop through each sublist and sum their lengths.

Example

In this example, we are using a loop to count the total number of cities in a nested list.

# Creating a nested list of cities
cities = [
    ["New York", "Los Angeles"],
    ["London", "Manchester"],
    ["Tokyo", "Osaka"]
]

# Calculating the total number of cities
total_cities = sum(len(region) for region in cities)

# Displaying the total count
print("Total number of cities:", total_cities)

The output of the above code would be:

Total number of cities: 6

Exercise

Select the correct option to complete each statement about getting the length and counting elements in a list in Python.

  1. Which function is used to get the number of items in a list?
  2. Given my_list = [1, 2, 2, 3, 2], what does my_list.count(2) return?
  3. If items = ['a', 'b', 'c', 'a'], what is the result of items.count('a')?

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.