Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs

Python List Length

by suresh

How to use this len function to find the Python list length with few practical examples?. The Python len is used to find the length of any object. Here, we used Python len function to find list length.

The syntax of this len function on List to find python list length is:

len(list_name)

Python List length Example

In this Python list length example, we declared an empty list. Next, we used the len function to find the length of an empty List.

# Python List Length Example

# Python Length of an Empty List
emptyList = []
print("Length of a List = ", len(emptyList))
Python List Length 1

List Length of an Integer Example

We are finding the length of an integer list using list len function..

# Python List Length Example

# Python Length of an Integer List
integerList = [12, 22, 33, 44, 55, 66, 77]
print("\n Original List = ", integerList)
      
print("Length of an Integer List = ", len(integerList))
Python List Length 2

String List length Example

When you use this len function on String List, it returns the total number of words in a string. Or, say, it finds the length of the string items in a list.

This Python example shows the total number of string items or the total number of words in the string List.

# Python List Length Example

# Python Length of a String List
stringList = ['Krishna', 'John', 'Yung', 'Ram', 'Steve']
print("\n Original List = ", stringList)
      
print("Length of a String List = ", len(stringList))
Python List Length 3

Python Mixed List Length Example

Apart from regular lists, you can also find the length of the mixed list.

# Python List Length Example

# Python Length of a String List
mixedList = ['Krishna', 20, 'John', 40.5, 'Yung', 11.98, 'Ram', 22]
print("\n Original List = ", mixedList)
      
print("Length of a Mixed List = ", len(mixedList))
Python List Length 4

Length of Mixed list Example 2

We declared a Tuple inside a list. Next, we are going to find List length (this includes tuple). Remember, len counts the complete Tuple as a single element.

# Python List Length Example

# Python Length of a String List
mixedList = ['Krishna', 20, 'John', (40, 50, 65), 'Yung', 11.98, 'Ram']
print("\n Original List = ", mixedList)
      
print("Length of a Mixed List = ", len(mixedList))
Python List Length 5

Python Nested List Length Example

Let us see how to find the length of a nested list. For this, we declared a list inside a list with the combination of a few other items. If you want the complete list length, then it considers the Nested list as one element. However, you can get the Nested list length using the index value. For example, the below code is finding the length of a nested list [20, 40, 50, 65, 22].

# Python List Length Example

# Python Length of a String List
nestedList = ['Krishna', 20, 'John', [20, 40, 50, 65, 22], 'Yung', 11.98]
print("\n Original List = ", nestedList)
      
print("Length of a Nested List = ", len(nestedList[3]))
Python List Length 6

Dynamic List Length Example

This Program allows the user to enter the total number of list items. Next, using For Loop, it iterates each list position and allows you to enter the individual list items. Within the loop, we used append function to add items to List. Once we got the list items, we are finding the list length using this len function.

# Python List Length Example

intList = []
Number = int(input("Please enter the Total Number of Items in a List : "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    intList.append(value)
    
print("\n Original List = ", intList)
      
print("Length of a Dynamic List = ", len(intList))
Python List Length 7

Placed Under: Python, Python Examples

  • Download and Install Python
  • Python Arithmetic Operators
  • Python Assignment Operators
  • Python Bitwise Operators
  • Python Comparison Operators
  • Python Logical Operators
  • Python If Statement
  • Python If Else
  • Python Elif Statement
  • Python Nested If
  • Python For Loop
  • Python While Loop
  • Python Break
  • Python Continue
  • Python Dictionary
  • Python datetime
  • Python String
  • Python Set
  • Python Tuple
  • Python List
  • Python List Comprehensions
  • Python Lambda Function
  • Python Functions
  • Python Types of Functions
  • Python Iterator
  • Python File Handling
  • Python Directory
  • Python Class
  • Python classmethod
  • Python Inheritance
  • Python Method Overriding
  • Python Static Method
  • Connect Python and SQL Server
  • Python SQL Create DB
  • Python SQL Select Top
  • Python SQL Where Clause
  • Python SQL Order By
  • Python SQL Select Statement
  • Python len Function
  • Python max Function
  • Python map Function
  • Python print Function
  • Python sort Function
  • Python range Function
  • Python zip Function
  • Python Math Functions
  • Python String Functions
  • Python List Functions
  • Python NumPy Array
  • NumPy Aggregate Functions
  • NumPy Arithmetic Operations
  • Python Numpy Bitwise operators
  • Numpy Comparison Operators
  • Numpy Exponential Functions
  • Python Numpy logical operators
  • Python numpy String Functions
  • NumPy Trigonometric Functions
  • Python random Array
  • Python numpy concatenate
  • Python numpy Array shape
  • Python pandas DataFrame
  • Pandas DataFrame plot
  • Python Series
  • Python matplotlib Histogram
  • Python matplotlib Scatter Plot
  • Python matplotlib Pie Chart
  • Python matplotlib Bar Chart
  • Python List Length
  • Python sort List Function
  • Python String Concatenation
  • Python String Length
  • Python substring
  • Python Programming Examples

Copyright © 2021· All Rights Reserved.
About | Contact | Privacy Policy