Tuesday, October 27, 2015

Python Tutorial: Extend List Function With Range Function - Python List #82



Find The Length Of A List In Python

Find The Length Of A List In Python

In this Python tutorial we will take a look at how to find the length of a list in Python. Finding the length of a list in Python is very similar to finding the length of a string in Python. We simply use the Python len built-in function on our list and we will be returned the amount of objects contained in the list. Len returns the amount of objects in a integer format.

Len Built-in Function On A List Syntax

len(list argument)

  • len() - The len built-in function will return a integer to us indicating how many objects are contained in a list.
  • list argument - This is where we place our list argument which can be a list or variable that represents a list or the list built in function.

Examples Of Len Built-in Function On List

Example of Len Function On List

>>> len([1,2,3,4,5])
5
  • len([1,2,3,4,5]) - We call the len built-in function on our list object. Our list object contains five other integer objects.
  • 5 - We are returned an integer of 5 which means there is 5 objects in our list.

Example of Len Function On List Variable

>>> a = ['Hello', 'World']
>>> len(a)
2
  • a = ['Hello', 'World'] - We create a new list object that contains two string objects. We assign 'a' to represent the list object.
  • len(a) - We call the length built-in function on our list via the variable 'a'.
  • - We are returned 2 which indicates that we have two objects contained in our list.

Conclusion

In this tutorial we have learned how to find the length of a list in Python. If you have any questions about the length built-in function and list leave a comment below.

No comments:

Post a Comment