Find the Length of a List in Python

Published on October 2, 2020 27 sec read

HIRE US 🖐
We're available to do freelance project. Take a look at our services!

A list in Python is used to store the sequence of various types of data. It is one of the most commonly used data types in Python. This article shows how to find the length of a list.

Table of Contents

  1. Using len() Function
  2. Using Loop

Using len() Function

The syntax of the len() function:

len(list)

Let’s have a look at an example:

list_capital = ['Dhaka', 'London', 'Tokyo']

length = len(list_capital)

print("The length of the list is: {0}" . format(length))

Using Loop

We can do the same thing using for loop.

list_capital = ['Dhaka', 'London', 'Tokyo']

count = 0
for capital in list_capital:
    count = count + 1

print("The length of the list is: {0}" . format(count))

We should always prefer using the len() function.

That’s it. Thanks for reading. 🙂

Author

Hey, I'm Md Obydullah. I build open-source projects and write on Laravel, Linux server, modern JavaScript and more on web development.

Follow

Leave a Reply

Your email address will not be published. Required fields are marked *