What Are Root Nameservers? How DNS Root Servers Work
Every time you enter a website address into your browser, the Domain Name System (DNS)…
In Python, a list is a way to store multiple items in a single variable. For example, you might have a list of numbers, names, or even a mix of different types of data. Sometimes, you need to know how many items are in a list. This is called the length of the list.
Python makes it very easy to find the length using a built-in function called len(). Let’s go through this step by step.
A list is a collection of items written inside square brackets []. Items are separated by commas.
numbers = [5, 10, 15, 20]
This list has 4 items: 5, 10, 15, and 20.
Knowing the length of a list is useful when:
len() FunctionPython provides a simple function called len() to count the number of items in a list.
Syntax:
You can also store the result of len() in a variable if you want to use it later in your program.
Example:
fruits = [“apple”, “banana”, “cherry”]
count = len(fruits)
print(“There are”, count, “fruits in the list.”)
Output:
There are 3 fruits in the list.
len() Works with Other Data Types TooThe len() Function is not just for lists. It also works with:
Example:
len(“hello”) # Output: 5
len((1, 2, 3)) # Output: 3
len({“a”: 1, “b”: 2}) # Output: 2
This article introduced various methods to determine the length of a list in Python.
Find more information about how to check Python Version in Linux & Windows
Explore more hosting insights, tips and industry updates.
Every time you enter a website address into your browser, the Domain Name System (DNS)…
Firewalld is a lightweight, command-line firewall tool normally used on Linux systems. While it is…
We’ll show you how to configure customer support requests in WHM in this article. bodHost…