Python length of list, It is our target to learn today. For a beginner it raises some questions.
What is meant by length of list?
As you know that! List is a data structure which retains an ordered sequence of elements. List is mutable, that means any element(also known as item) in a list is changeable(insertable/deletable/replaceable).
Length of list means the number of items currently a list has. i.e if insert or append 10 items in a list then the length of list will be 10.
Python length of List
Python provides many builtin methods to manipulate a list object. To find the “Python length of list” or in other words “total number of items in a python list” a method “len()” can be used or we also have an iterative approach to fulfill our target.
Find Python length of list using python len method
Python list method gives number of items in a sequence whether it is a python list or python dictionary or a python tuple, regardless of which data type items they contain.
>>>py_list = [1, 5, 9, 4, 3, 9, 20, 25] >>>len(py_list) 8 >>>py_list = [1, 9, 'x' ,'z', "abc", "sample"] >>>len(py_list) 6
Find Python length of list using iterative approach
As list is a sequential data structure and all sequences are iterable. So we can iterate a list using for loop, to find the total number of items in it.
>>>py_words_list = ['python', 'length', 'of', 'list'] >>>counter = 0 >>>for word in py_words_list: >>> counter = counter + 1 >>>print(counter) 4