26
loading...
This website collects cookies to deliver better user experience
[]
, and the elements in the list are separated using commas.# List with integers
numbers = [1, 2, 3, 4, 5]
# List with string
fruits = ["apple", "orange", "grapes"]
# List with mixed data type
mixedlist = [1, "Ninja", 55.22, False,"a"]
len()
methodlen()
method accepts the list as an argument and returns the length of the list in Python.len(list)
len()
method is the most widely used and convenient way to get the length of the list in Python. The below example will print the length of a list.# List with integers
numbers = [1, 2, 3, 4, 5]
# List with string
fruits = ["apple", "orange", "grapes"]
# List with mixed data type
mixedlist = [1, "Ninja", 55.22, False,"a"]
print("Length of numbers = ", len(numbers))
print("Length of fruits = ", len(fruits))
print("Length of mixed list = ", len(mixedlist))
# Output
Length of numbers = 5
Length of fruits = 3
Length of mixed list = 5
mixedlist = [1, "Ninja", 55.22, False,"a"]
print("The current list is : ",mixedlist)
count=0
for i in mixedlist:
count=count+1
print("Length of mixed list = ", count)
# Output
The current list is : [1, 'Ninja', 55.22, False, 'a']
Length of mixed list = 5
len()
method, or you can use the iteration way, i.e., using for loop. I hope you have understood how to find the length of any list in Python.