About usWhy usInstructorsReviewsCostFAQContactBlogRegister for Webinar

Find Size of a List in Python

In Python, List is one of the most frequently used and versatile data types. Knowing how to harness the power of a list’s versatile nature can be useful on innumerable occasions. If you’re a software engineer relatively new to Python programming, this article can help you move towards your Python learning goals.

In this article, we’ll learn:

  • What Is a List in Python?
  • The len() Function in Python
  • Finding the Size of a List Using len() Function
  • Finding the Size of a List Using for Loop
  • FAQs

What Is a List in Python?

A list in Python is a collection data type used to store several values using a single variable. The values in the list may or may not be of the same type. However, it is usually used to store values of the same type, which represent some collection. It can also store duplicate values. A list is ordered and changeable and is represented by writing values separated by commas, stored within square brackets as shown below.

listOfOdd=[1, 3, 5, 7, 9]

listOfChar=['a', 'b', 'c', 'd']

listOfMix=['a', 'b', 1, 3]

The len() Function in Python

len() is a built-in function in Python that returns the length of an object like a list, string, dictionary, set, tuple, etc. The len() function takes one argument: an object which may be a sequence or a collection, and returns an integer, which is the number of items in the object, i.e., its size. len() performs this operation in O(1) time.

Syntax:

len(object)

Example:

listEx = [1, 2, 3, 'a','b','c']

stringEx='InterviewKickstart'

setEx={0,2,4,6,8}

tupleEx=(0,'Zero',1,'One')

dictionaryEx={'Zero': 0, 'One':1, 'Two':2}

print(len(listEx))

print(len(stringEx))

print(len(setEx))

print(len(tupleEx))

print(len(dictionaryEx))

Output:

6

18

5

4

3

Finding the Size of a List Using len() Function

The most commonly used method of finding the size of a list is using the len() function. The built-in class list also has a method __len__(), which also returns the size/number of items in the list. 

The len() function actually works by calling the object’s __len__() method. Since attributes in the format __xyz__ often are more complex than we realize, their direct use isn’t encouraged. Hence we prefer the len() method for finding the size of a list. Let us look at both of these methods through an example.

Example:

Code

#adding some elements in an empty list

listEx = []

listEx.append(1)

listEx.append(3)

listEx.append(5)

listEx.append('seven')

listEx.append('nine')


#getting the size of the list after adding multiple elements

print(len(listEx))

print(listEx.__len__())

Output

5

5

Finding The Size of a List Using for Loop

We can also find the size of a list by simply using a for loop, creating a count variable, iterating through all the items in the list, and incrementing the value of count by one in each iteration. 

Since this method isn’t very pythonic, we again prefer the len() function for finding the size of a list. We can make use of a for loop to find the length of each element in a list that only stores objects while using the len() function. Let us see how we can use this method through an example.

Example:

Code

listEx = [1, 2, 3, 'a','b','c','Interview', 'Kickstart']

count = 0


for item in listEx:

  count = count + 1

print('The size of the list is: ', count)

 # When elements in a list are also objects, we can also use a for loop to calculate the length of each element in the list,

# while we use the len() function to find the length of the 

# list

listEx2=['Interview', 'Kickstart']

for item in listEx2:

  print(item, ' is of size ', len(item))

print ('Length of the list  = ', len(listEx2))

Output

The size of the list is:  8

Interview  is of size  9

Kickstart  is of size  9

Length of the list  =  2

FAQs 

Question 1: Can we use the len() function in a for loop to find the size of string elements in a list that also contains items that are not objects (like int, char, etc.)?

Example:

listEx = ['a','b','c', 1 ,'Interview', 'Kickstart']

Answer: No. len() takes an object as its argument, and we’ll get a type error since int, char, etc., are not objects.

Question 2: Can we use the len() function in a for loop to find the size of string elements in a list that also contains items that are objects but of different types (like a dictionary, tuple, etc.)

Example:

listEx = [{'a':1,'b':1,'c':1},'Interview', 'Kickstart']

Answer: Yes. If the list contains elements of different data types, but all are objects that are sequences or collections, we can use the len() function to find the size of each element.

Recommended Reading

Are you a software engineer looking for coding interview questions to aid your interview prep? Check out these useful pages for software developers:

Are You Ready to Nail Your Next Coding Interview?

Whether you’re a Coding Engineer gunning for Software Developer or Software Engineer roles, or you’re targeting management positions at top companies, IK offers courses specifically designed for your needs to help you with your technical interview preparation!

If you’re looking for guidance and help with getting started, sign up for our free webinar. As pioneers in the field of technical interview prep, we have trained thousands of Software Engineers to crack the most challenging coding interviews and land jobs at their dream companies, such as Google, Facebook, Apple, Netflix, Amazon, and more!

Sign up now!

————

Article contributed by Tanya Shrivastava

Attend our Free Webinar on How to Nail Your Next Technical Interview

Recommended Posts

All Posts