3 Examples to Get List Length in Python 3

Table of Contents

List in python language is used to store multiple elements in a single variable.

  • Lists are one of the 4 data types in python language along with tuple, set, and dictionary.
  • List in Python is created by putting elements inside the square brackets, separated by commas.
  • A list can have any number of elements and it can be of multiple data types.
  • Also, all the operation of the string is similarly applied on list data type such as slicing, concatenation, etc.
  • Also, we can create the nested list i.e list containing another list.

In this article, we will cover how to get the length of a python list with 3 examples.

3 Examples to Get List Size in Python 3

Get list length in Python 3 Examples
len() function list = [10, 20, 30]
n = len(list)
print(“The length of list is: “, n)
for loop list = [‘Python’,’Java’,’Kotlin’,’Machine Learning’,’Keras’]
size = 0
print(“Length of the input string:”)
for x in list:
size+=1
print(size)
len() function for nested list nestedList = [‘Krishna’, 20, ‘John’, [20, 40, 50, 65, 22], ‘Yung’, 11.98]
print(“Original List = “, nestedList)

print(“Length of a Nested List = “, len(nestedList[3]))

Get the length of the list with len() function

To get the length of the list in python we can use the len() function.

Len() Function is an inbuilt function in python and returns a number of elements present in the list object.

Example of get size of list Python
# Python program to demonstrate working
list1 = [10, 20, 30]
n = len(list1)
print(“The length of list is: “, n)

Output: 3

Using Python for loop to get the length of a list

In order to find the length of the list in Python, using for loop is considered as a traditional technique or a naive method in the following manner:

  • Declare a counter variable and initialize it to zero.
  • Using a for loop, traverse through all the data elements and after encountering every element, increment the counter variable by 1.
  • Thus, the length of the array will be stored in the counter variable as the variable will represent the number of elements in the list.

counter = 0
for item in list:
counter+=1
print(counter)

Example:

inp_lst = [‘Python’,’Java’,’Kotlin’,’Machine Learning’,’Keras’]
size = 0
print(“Length of the input string:”)
for x in inp_lst:
size+=1
print(size)
Output:

Length of the input string:
5

Why should we prefer len() to get the length?

we should prefer the len() function to determine the length of a list rather than iterating through the list every time we require to determine its length because –

  • Simplicity. Using len() is quite simple as against using a loop. Not only does it reduce the code to fewer lines, but it’s also very readable.
  • The len() function is O(1) in time complexity whereas using a loop is O(n). This means that the len() function is very quick in determining the length of a list.
  • It takes constant time and does not depend on the size of the list itself. This is possible because objects like lists track their own length and every time we use the len() function, it basically “looks up” for the length attribute of the object and returns it.

 

Find the length of a nested list

Let us see how to find the length of a nested list. For this, we declared a list inside a list with the combination of a few other items. If we want the complete list length, then it considers the Nested list as one element. However, we can get the Nested list length using the index value. For example, the below code is finding the length of a nested list [20, 40, 50, 65, 22].

# Length of a String List
nestedList = [‘Krishna’, 20, ‘John’, [20, 40, 50, 65, 22], ‘Yung’, 11.98]
print(“\n Original List = “, nestedList)

print(“Length of a Nested List = “, len(nestedList[3]))
Original List = [‘Krishna’, 20, ‘John’, [20, 40, 50, 65, 22], ‘Yung’, 11.98]
Length of a Nested List = 5

Interview Questions

Here are some common interviews questions on get size of list Python.

Q: What is “len function” time complexity?
Answer: len() works in O(1) time as list is an object.

Q: How do I get the number of elements in a list?
Answer: Use python length method “len()” to get the size of list. See below example of it.

# empty list
items = []
items.append(“apple”)
items.append(“orange”)
items.append(“banana”)

# Get and print number of elements in the list
print(len(items))

Output: 3

Q: What is the best way to find the length of a list in Python?
Answer: The best way to find the length of List in python use Len() function.

Q: How to get the Python length of the Array?
Answer: Python doesn’t have an array data type. Python is using a “List” data type instead of an Array. Both works the same but there are some differences.

Where Array is using in Java, C++ and other programming language and Lists are used in Python.

 

Table of Contents

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn

You might also like