Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

How to Find the Length of a List in Python - DesignLinux

Sep 29 2020
designlinux 0 Comments
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.

Related

Tags: python

How to Install Python IDLE in Linux

Prev Post

Heartbleed Still Found in the Wild: Did You Know That You May Be Vulnerable?

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 0
  • 1,017
  • 609,465

DesignLinux.com © All rights reserved

Go to mobile version