Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

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

designlinux 0 Comments

Lists are one of the most commonly used data types in Python and are used to store collections of items of the same type.

This article shows how to find the length of a list.

len() Function #

Python has a built-in function len() that returns the length of a given object. The object can be a list , tuple, string, dictionary, etc.

The syntax of the len() function is as follows:

len(list)

The function accepts only one argument. The returned value is an integer that is the number of elements in the list.

Here is an example:

capitals = ['Tokyo', 'Sofia', 'London', 'Budapest']

list_len = len(capitals)

print("The list has {0} elements.".format(list_len))
The list has 4 elements.

Using Loop #

Another way to get the length of a list is to use the for loop. This works by setting up a counter and looping through all the elements of the list. On each iteration, the current value of the counter variable is incremented by one.

Below is a sample code that shows how to find the number of elements in a given list using the for loop:

capitals = ['Tokyo', 'Sofia', 'London', 'Budapest']
counter = 0

for capital in capitals:
  counter = counter + 1

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

This method is not very Pythonic. You should always prefer using the len() function.

Conclusion #

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.

python

Related

Tags: python

How to Mount Windows Share on Linux using CIFS

Prev Post

How to Install Roundcube Webmail on CentOS/RHEL 8/7

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
  • 72
  • 605,822

DesignLinux.com © All rights reserved

Go to mobile version