Count the number of elements in the Python list | Laptrinhcanban.com

HOME › >>

Count the number of elements in the Python list

Posted at: https://laptrinhcanban.com/en

Instructions Count the number of elements in the Python list . You will learn how to use the len () function to count the number of elements in a Python list after this lesson.

Count the number of elements in the Python list

To count the number of elements in the Python list, we use the len () function with the following syntax:

len (list)

Len () in python is a function used to find the length or number of elements of an object in python. If this object is list, the function will output the number of elements in the specified Python list.

Example of counting the number of elements in a Python list

len ([ "Red" , "Green" , "Blue" ]) 
len ([ 1 , 2 , 3 , 4 , 5 ])
len ([ 10 , 20 , [ "Sun" , "Mon" , "Fri" ] , 40 ])

Result:

3
5
4

How to Count the number of elements in Python list using len () function is quite simple. Finally, you should practice online to use it more proficiently.

print ( len ([ "Red" , "Green" , "Blue" ]))

print ( len ([ 1 , 2 , 3 , 4 , 5 ]))

print ( len ([ 10 , 20 , [ "Sun" , "Mon" , "Fri" ], 40 ]))

Summary

Above, Kiyoshi showed you how to count the number of elements in the Python list using the len () function. To better understand the lesson content, practice rewriting today’s examples.

And let’s learn more about Python in the next lessons.

URL Link

https://laptrinhcanban.com/en/python/nhap-mon-lap-trinh-python/list-trong-python/dem-so-phan-tu-trong-list-python/

Thank you for reading and please Like & Share for other friends to learn.
">

HOME  › >>

  • Recent Posts
Profile
きよしです!笑

Author: Kiyoshi (Chis Thanh)

Kiyoshi was a former international student in Japan. After graduating from Toyama University in 2017, Kiyoshi is currently working at BrSE in Tokyo, Japan. Kiyoshi là một cựu du học sinh tại Nhật Bản. Sau khi tốt nghiệp đại học Toyama năm 2017, Kiyoshi hiện đang làm BrSE tại Tokyo, Nhật Bản.