Author
Dipen Dadhaniya
Engineering Manager at Interview Kickstart
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:
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]
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
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:
#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__())
5
5
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:
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))
The size of the list is:Â 8
Interview is of size 9
Kickstart is of size 9
Length of the list = 2
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.
Are you a software engineer looking for coding interview questions to aid your interview prep? Check out these useful pages for software developers:
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!
————
Article contributed by Tanya Shrivastava
Time Zone:
Get your enrollment process started by registering for a Pre-enrollment Webinar with one of our Founders.
The 11 Neural “Power Patterns” For Solving Any FAANG Interview Problem 12.5X Faster Than 99.8% OF Applicants
The 2 “Magic Questions” That Reveal Whether You’re Good Enough To Receive A Lucrative Big Tech Offer
The “Instant Income Multiplier” That 2-3X’s Your Current Tech Salary
The 11 Neural “Power Patterns” For Solving Any FAANG Interview Problem 12.5X Faster Than 99.8% OF Applicants
The 2 “Magic Questions” That Reveal Whether You’re Good Enough To Receive A Lucrative Big Tech Offer
The “Instant Income Multiplier” That 2-3X’s Your Current Tech Salary
Just drop your name and email so we can send your Power Patterns PDF straight to your inbox. No Spam!
By sharing your contact details, you agree to our privacy policy.
Time Zone: Asia/Dhaka
We’ve sent the Power Patterns PDF to your inbox — it should arrive in the next 30 seconds.
📩 Can’t find it? Check your promotions or spam folder — and mark us as safe so you don’t miss future insights.
We’re hosting a private session where FAANG insiders walk through how they actually use these Power Patterns to crack interviews — and what sets top performers apart.
🎯 If you liked the PDF, you’ll love what we’re sharing next.
Time Zone: