# 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.

### 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:

`# get the length of a list using the len() function# program to count items in the list programming_list = ["Learn Java", "Learn C++",                    "Learn Python", "Learn C#", "Learn JavaScript", "Learn PHP"]list_length = len(programming_list)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:

`# program to count elements in the list using the FOR loop statementelement_in_lists = ["Microsoft", "Google", "Oracle",                    "Yahoo", "Facebook", "Twitter", "Sony"]count = 0for element in element_in_lists:    count += 1print("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:

`# program to count items in the list using the WHILE loop statementitem_in_lists = ["Item 001", "Item 002", "Item 003",                 "Item 004", "Item 005", "Item 006", "Item 007", "Item 008"]count = 0while (item_in_lists[count:]):    count += 1print("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:

`# count unique values in the list using set() and len()number_lists = [10, 20, 10, 30, 40, 20, 40, 50, 60, 30, 80]unique_numbers = set(number_lists)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:

`# count unique values in the list using the FOR loop and IF statement number_lists = [10, 20, 10, 30, 40, 20, 40, 50, 60, 30, 80]unique_count = 0unique_numbers = []for number in number_lists:    if number not in unique_numbers:        unique_numbers.append(number)        unique_count += 1print("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:

`# count occurrences of all items in the listitem_in_lists = ["Item 001", "Item 002", "Item 001", "Item 001",                 "Item 002", "Item 003", "Item 002", "Item 003", "Item 002"]item_dict = {    i: item_in_lists.count(i) for i in item_in_lists}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:

`# count occurrences of all items in the listitem_in_lists = ["Item 001", "Item 002", "Item 001", "Item 001",                 "Item 002", "Item 003", "Item 002", "Item 003", "Item 002"]item_dict = dict((i, item_in_lists.count(i)) for i in item_in_lists)print(item_dict)`

Output:

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

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

Another example:

`# count occurrences of all items in the list item_in_lists = ["Item 001", "Item 002", "Item 001", "Item 001",                 "Item 002", "Item 003", "Item 002", "Item 003", "Item 002"]unique_item_in_lists = set(item_in_lists)item_dict = {}for element in unique_item_in_lists:    item_dict[element] = item_in_lists.count(element)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:

`# count occurrences of all items in the list item_in_lists = ["Item 001", "Item 002", "Item 001", "Item 001",                 "Item 002", "Item 003", "Item 002", "Item 003", "Item 002"]item_dict = {}for element in item_in_lists:    if element in item_dict:        item_dict[element] += 1    else:        item_dict[element] = 1print(item_dict)`

Output:

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

#### Using Counter

`from collections import Counter# count occurrences of all items in the list item_in_lists = ["Item 001", "Item 002", "Item 001", "Item 001",                 "Item 002", "Item 003", "Item 002", "Item 003", "Item 002"]item_dict2 = dict(Counter(item_in_lists))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.