Python List Length – Find the Length of a List


How to Find Length of a List in Python

In this tutorial, you'll learn how to find the length of a given list in Python. This tutorial describes how to count elements in the list using the built-in functions, and also provides sample programs to illustrate how to count items, count occurrences of all the items in the list, and count unique items in the list.

Also, I show how you can use the FOR loop and WHILE loop statements to achieve the same result.

Find the Length of a List in Python

You can find the length of a list in Python using different ways.

Using the built-in len() Function

The following program illustrates how to get the length of the 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

Find the Length of a List using FOR Loop

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

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

Output:

The number of elements in the list is 7

Count Elements Using WHILE Loop

The following sample program counts the number of items in the list using the WHILE loop statement:

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

Output:

The number of items in the list is 8

Count Unique Values in a List

In this section, I show you how to count the number of unique items in a list.

There are several ways that you can count the number of unique items in a list.

Count Unique Values using set() and len()

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 = [1020103040204050603080]
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

Count Unique Values using FOR Loop and IF Statement

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

# count unique values in the list using the FOR loop and IF statement
number_lists = [1020103040204050603080]
unique_count = 0
unique_numbers = []
for number in number_lists:
    if number not in unique_numbers:
        unique_numbers.append(number)
        unique_count += 1
print("The number of unique elements in the list: ", unique_count)

Output:

The number of unique elements in the list: 7

Count Occurrences of all Items in a List

Example 1: Using List count() Method and FOR Loop (1)

The following program illustrates how to count occurrences of all the item using the list count() method and FOR loop statement:

# 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 = {
    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}

Example 2: Using List count() Method and FOR Loop (2)

The following program illustrates how to count occurrences of all the item using the list count() method and FOR loop statement:

# 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 = 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}

Example 3: Using List count() Method and FOR Loop (3)

The following program illustrates how to count occurrences of all the item using the list count() method and FOR loop statement:

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

Example 4: Using FOR Loop and IF ELSE

The following program illustrates how to count occurrences of all the item using the FOR loop and IF-ELSE statement:

# 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] = 1
print(item_dict)

Output:

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

Example 5: 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}

In this tutorial, you've learned how to find the length of a given list. Also, this tutorial described how to count elements in the list, count occurrences of all the items, and count unique items in the list with examples. We've used the built-in functions, the WHILE loop, and FOR loop statements to achieve the same result.

Leave a Reply

Your email address will not be published. Required fields are marked *