• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

How to Find the Length of a List in Python

Written by Admin, Updated On September 29, 2020
python
How to Find the Length of a List in Python

In Python, lists are the mostly used data type to store collections of the same type data. This guide shows you how to find the length of a list in Python.

len() Function#

The len() function is a built-in function of Python, used to get the length of a specified object. It can be a list , tuple, string, dictionary, etc.

Following is the basic syntax of the len() function:

len(list)

It accepts only one argument and returns integer number.

Let’s see an example,

os = ['Ubuntu', 'Debian', 'CentOS']

len_of_list = len(os)

print("The list has {0} elements.".format(len_of_list))
The list has 3 elements.

Using Loop#

You also can get the length of a list using the for loop. You should set up a counter and execute the loop for all the elements of the object. It will do incremented in the current value of the counter variable.

Below is the code sample to find the number of elements of list using for loop:

os = ['Ubuntu', 'Debian', 'CentOS']
counter = 0

for s in os:
  counter = counter + 1

print("The list has {0} elements.".format(counter))
The list has 3 elements.

The first method is best to use.

Conclusion#

You learned how to find the length of a list in Python using len() function.

To find the length of a list in Python list, use the len() function.

If you have any questions or feedback, feel free to leave a comment.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article How to Kill a Process in Linux Systems
Next Article   Pgrep Command in Linux

Related Posts

  • How to Install Python on Ubuntu 22.04

    How to Install Python on Ubuntu 22.04

    February 18, 2023
  • How to Install Python 3.11 on Debian 11

    How to Install Python on Debian 11

    January 25, 2023
  • How to Install Python 3.9 on CentOS 8

    How to Install Python 3.9 on CentOS 8

    December 31, 2020

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install Microsoft Edge Browser on Ubuntu 22.04
    How to Install Microsoft Edge Browser on Ubuntu 22.04 March 14, 2023
  • How to Install Ruby on Ubuntu 22.04 LTS
    How to Install Ruby on Ubuntu 22.04 LTS February 27, 2023
  • How to Install PHP 8.2 on Debian 11
    How to Install PHP 8.2 on Debian 11 Linux February 24, 2023
  • How to Install LEMP Stack on Ubuntu 22.04
    How to Install LEMP Stack on Ubuntu 22.04 March 18, 2023
  • How to Install Set Up Apache Virtual Hosts on Ubuntu 22.04
    How to Set Up Apache Virtual Hosts on Ubuntu 22.04 March 2, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook