Get the App
SLTechnology News&Howtos  ›  Development  › 

How does python get the list length

Shulou Source: shulou.com Published: 2022-06-01 18:49:07 10月04日 Update

This article mainly introduces the relevant knowledge of "how to obtain list length by python". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to obtain list length by python" can help you solve the problem.

Preface

Python is a very expressive language that provides different structures to simplify the work of developers. This list is one of the most popular data structures provided by python. In a regular workflow, we add or remove elements from the list. But in the case of this float, we need to get the length of the list.

First, use the built-in len () function

As mentioned earlier, len is a built-in function provided by python by default. We can use this feature to provide only the list as a parameter, as shown below. The syntax is very simple, we only provide len (), list, array, tuple, or dictionary type variables as arrays.

Name_list= ['ismail','ahmet','ali'] len (name_list) fruit_list = [' apple', 'banana',' carrot', 'melon',' tomato'] len (fruit_list) number_list = [1, 2, 3, 4, 5, 7, 8, 9, 10, 10, 12, 14, 15] len (number_list) 2, using the built-in function len ()

Use the built-in len () function to get the multidimensional list length

In reality, there will be a multidimensional list. We can also obtain the length of the one-dimensional length of the list simply by providing an index of the relevant sublist, as shown below. In this example, we want to get the length of the first subarray.

Name_list= [['ismail','elif'],' ahmet','ali', [7 apple', 'banana',' carrot', 'melon',' tomato']] len (name_list) len (name_list [0]) len (name_list [7]) 3. Get the multidimensional list length

Get the multidimensional list length

We provide a sublist element index that this is ['ismail','elif'] and set the length of this sublist to 2

Use the For loop to calculate the length by iterating through each element

The len () function provides a very convenient, easy, and efficient way to get the length or size of an array. But in some cases, we may want to calculate the length or size of the list one by one. Even we want to eliminate some elements from the list and not count them. In this example, we can use a for loop through the for list.

Name_list= ['ismail','ahmet','ali'] count=0for element in name_list:count=count+1print (count) name_list= [[' ismail','elif'], 'ahmet','ali', [7, 8, 9, 10], 1, 2, 3, [' apple', 'banana',' carrot', 'melon',' tomato']] count=0for element in name_list:count=count+1print (count) Count Length with For Loop By Iterating Each ElementCount Length with For Loop By Iterating Each Element

Use the For loop to calculate the length dictionary length by iterating over each element

The len () function is useful when getting the length or size of different array types such as dictionaries. We can use the same syntax to calculate dictionary keys and value type elements. This counts 1 key and value pair.

That's all for name_surname= {'ismail':'baydan','ahmet':'baydan','ali':'baydan'} len (name_surname) about "how to get list length from python". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Length element function array dictionary case multidimensional different size knowledge type loop is in example structure industry grammar work iteration valid Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Docker Apple Linux NVidia