Join Regular Classroom : Visit ClassroomTech

len() function in Python List

It is a function in python that returns the length of the list, which is a positive integer value. It counts each and every element in the list and counting for length starts from 1.

Syntax:

len(list_name)

Don’t know what is sum function in Python List? Read: sum() function in Python List

Example 1:

# Python program to understand len() function in list
# www.codewindow.in

s = [85, 55, 47, 98, 32, 65, 62, 54] 
x = len(s)
print(x)

Output:

8

Explanation:

Here the length of the list is 8 starting from 1 (The list has 8 elements in it). Hence it returned 8.

Example 2:

# Python program to understand len() function in list
# www.codewindow.in

s = ["Code", "and", "hustle"] 
x = len(s)
print(x)

Output:

3

Explanation:

Here the length of the list is 3 (has 3 elements in the list). Hence it returned 3.


You Missed

Also Checkout