Python List Length – Find the Length of a List

How to Find Length of a List in Python

In this tutorial, you will learn how to find the length of a list in Python. The tutorial explains how to count the elements of a list using built-in functions and provides sample programs that illustrate how to count items, count the number of occurrences of all items and count unique items.

This tutorial will also show you how to use the FOR loop and WHILE loop statements to obtain the same results.

Learn more:

Finding the Length of a List in Python

In Python, you can find the length of a list in a variety of ways.

Using the built-in len() function

The following program demonstrates how to get the length of a list using the built-in len() function:

  1. # get the length of a list using the len() function
  2. # program to count items in the list
  3.  
  4. programming_list = ["Learn Java", "Learn C++",
  5. "Learn Python", "Learn C#", "Learn JavaScript", "Learn PHP"]
  6. list_length = len(programming_list)
  7. print("The length of the list is", list_length)

Output:

The length of the list is 6

Using a FOR loop to find the length of the list

The following example shows how to count the number of elements in a list using a FOR loop statement:

  1. # program to count elements in the list using the FOR loop statement
  2. element_in_lists = ["Microsoft", "Google", "Oracle",
  3. "Yahoo", "Facebook", "Twitter", "Sony"]
  4. count = 0
  5. for element in element_in_lists:
  6. count += 1
  7. print("The number of elements in the list is", count)

Output:

The number of elements in the list is 7

Counting elements using the WHILE loop

The following sample program uses a WHILE loop statement to count the number of items in a list:

  1. # program to count items in the list using the WHILE loop statement
  2. item_in_lists = ["Item 001", "Item 002", "Item 003",
  3. "Item 004", "Item 005", "Item 006", "Item 007", "Item 008"]
  4. count = 0
  5. while (item_in_lists[count:]):
  6. count += 1
  7. print("The number of items in the list is", count)

Output:

The number of items in the list is 8

Counting unique values in a list

Here is how to count the number of unique items in a list.

There are several ways to count the number of unique items in a list.

Using set() and len() to count unique values

The following program illustrates how to count the number of unique values in a list using the set() and len() functions:

  1. # count unique values in the list using set() and len()
  2. number_lists = [10, 20, 10, 30, 40, 20, 40, 50, 60, 30, 80]
  3. unique_numbers = set(number_lists)
  4. print("The number of unique elements in the list: ", len(unique_numbers))

Output:

The number of unique elements in the list: 7

Counting unique values with FOR loops and IF statements

The following sample program demonstrates how to count unique values in a list using a FOR loop and IF statements:

  1. # count unique values in the list using the FOR loop and IF statement
  2.  
  3. number_lists = [10, 20, 10, 30, 40, 20, 40, 50, 60, 30, 80]
  4. unique_count = 0
  5. unique_numbers = []
  6. for number in number_lists:
  7. if number not in unique_numbers:
  8. unique_numbers.append(number)
  9. unique_count += 1
  10. print("The number of unique elements in the list: ", unique_count)

Output:

The number of unique elements in the list: 7

Counting the number of occurrences of all items in the list

Using the list count() method and FOR loop (1)

The following program shows you how to count the number of occurrences of all items using the list count() method and the FOR loop statement:

  1. # count occurrences of all items in the list
  2. item_in_lists = ["Item 001", "Item 002", "Item 001", "Item 001",
  3. "Item 002", "Item 003", "Item 002", "Item 003", "Item 002"]
  4. item_dict = {
  5. i: item_in_lists.count(i) for i in item_in_lists
  6. }
  7. print(item_dict)

Output:

{'Item 001': 3, 'Item 002': 4, 'Item 003': 2}

Using the list count() method and FOR loop (2)

The following program is another example that shows you how to count the number of occurrences of all items using the list count() method and the FOR loop statement:

  1. # count occurrences of all items in the list
  2. item_in_lists = ["Item 001", "Item 002", "Item 001", "Item 001",
  3. "Item 002", "Item 003", "Item 002", "Item 003", "Item 002"]
  4. item_dict = dict((i, item_in_lists.count(i)) for i in item_in_lists)
  5. print(item_dict)

Output:

{'Item 001': 3, 'Item 002': 4, 'Item 003': 2}

Using the list count() method and FOR loop (3)

Another example:

  1. # count occurrences of all items in the list
  2.  
  3. item_in_lists = ["Item 001", "Item 002", "Item 001", "Item 001",
  4. "Item 002", "Item 003", "Item 002", "Item 003", "Item 002"]
  5. unique_item_in_lists = set(item_in_lists)
  6. item_dict = {}
  7. for element in unique_item_in_lists:
  8. item_dict[element] = item_in_lists.count(element)
  9. print(item_dict)

Output:

{'Item 001': 3, 'Item 002': 4, 'Item 003': 2}

Using FOR loop and IF-ELSE statements

The following program illustrates how to count the number of occurrences of all items using the FOR loop and IF-ELSE statements:

  1. # count occurrences of all items in the list
  2.  
  3. item_in_lists = ["Item 001", "Item 002", "Item 001", "Item 001",
  4. "Item 002", "Item 003", "Item 002", "Item 003", "Item 002"]
  5. item_dict = {}
  6. for element in item_in_lists:
  7. if element in item_dict:
  8. item_dict[element] += 1
  9. else:
  10. item_dict[element] = 1
  11. print(item_dict)

Output:

{'Item 001': 3, 'Item 002': 4, 'Item 003': 2}

Using Counter

  1. from collections import Counter
  2. # count occurrences of all items in the list
  3.  
  4. item_in_lists = ["Item 001", "Item 002", "Item 001", "Item 001",
  5. "Item 002", "Item 003", "Item 002", "Item 003", "Item 002"]
  6. item_dict2 = dict(Counter(item_in_lists))
  7. print(item_dict2)

Output:

{'Item 001': 3, 'Item 002': 4, 'Item 003': 2}

Summary

In this Python tutorial, you learned how to find the length of a list. The tutorial also explained how to count the elements, count the number of occurrences of all items, and count the unique items. This tutorial also illustrated how to use the built-in functions, WHILE loops, and FOR loop statements to achieve the same results.


See also:
Python return Statement with Examples
Python List Methods and Functions with Examples
Python abs() – Absolute Value in Python
Python Factorial – Find the Factorial of a Number
Python min() – Find the Smallest Value in Python

Leave a Comment