Here we will learn how to find python list length. Below we discussed the python list and find its length.
What is list in python?
A list is an ordered, mutable data structure in python. It can store mixed types of data.
elements are represented using square brackets [].
Example1 : Python list length on integers
A list of integers is created below and printed its length using len().
list1=[1,2,3,4,5,6,7,8,9,10];
print('Length of list1 :', len(list1));
Length of list1 : 10
Example2 : Python list length of strings
A list of string is created and printed its length using len()
list1=['Ram','Mohan','Sohan','Rita','Nita','Ganesh','Suresh'];
print('Length of list1 :', len(list1));
Length of list1 : 7
Example3 : Python list length of boolean values
list of boolean values True and False are created and printed its size using len().
list1=[True, False, False,False, True];
print('Length of list1 :', len(list1));
Length of list1 : 5
Example3 : Python list length of mixed values
A mixed type of list is created with string, int, and other data types and its length is created using len().
list1=['Ram', 22, 'India',True, 60000.00];
print('Length of list1 :', len(list1));
Length of list1 : 5
Example4 : Python list length of Objects
A class student is created and its two objects s1 and s2 are created.
A list is created using two student objects and its length is printed using len.
Here student details are also printed using for loop.
class Student:
def __init__(self,rollno,name):
self.rollno=rollno;
self.name=name;
s1=Student(1,'Ram');
s2=Student(2,'Mohan');
list1=[s1,s2];
print('Length of list1 :', len(list1));
for obj in list1:
print(obj.rollno, obj.name);
Length of list1 : 2 1 Ram 2 Mohan
Read More
Increment and decrement operators in Python (+=, -=)
Armstrong number in python: Algorithm, Program