Learn and Learn

Learn And Learn - great place for tutorials, references and how-to

How to Get the Size of the List in Python

To get size of a Python list we use len() function. len() function gives the length or size of a list object of Python Programming Language, which means the number of items in the list object. Let’s understand len() method by example.

Examples of len() Function of list in Python


Output:
3
In the above example we used len() function to myList and it gives the size of myList. There are three elements in myList. Remember that we used object myList as parameter to len() function. Let’s take another example.

Output:
['aa', 'bb', 'cc']
['aa', 'bb', 'cc', 1, 3, 5, 7]
['aa', 'bb', 'cc', 1, 3, 5, 7, 'aa', 'bb', 'cc', 1, 3, 5, 7]
14

In the above example we create a continuously increasing list using append() and extend() method. And our items in the list reach up to 14 items when we print the size of the list.

Now lets calculate the size of a bit bigger list. Look at the following example len() method.

Output:
['aa', 'bb', 'cc']
['aa', 'bb', 'cc', 1, 3, 5, 7]
['aa', 'bb', 'cc', 1, 3, 5, 7, 'aa', 'bb', 'cc', 1, 3, 5, 7]
14

The size of bigger list: 13999986
In the above example we create such a big list, the items of which cannot be even presented over this post. But the len() method calculates the size of the list.
 

Loading...
LearnAndLearn.com © 2018 - All rights reserved