# 如何在Python中获取列表的长度和大小？

Python is a very expressive language that provides different structures to easy developers work. The list is one of the most popular data structures provided by the python. In a regular workflow, we add and remove elements into and from the list. But in this floating situations, we need to get the length of the list. How can we get the length or size of the list? In this tutorial, we will look at different ways to get a list of length.

Python是一种非常具有表现力的语言，它提供了不同的结构来简化开发人员的工作。 该列表是python提供的最受欢迎的数据结构之一。 在常规工作流程中，我们在列表中添加元素或从列表中删除元素。 但是在这种浮动的情况下，我们需要获取列表的长度。 我们如何获得列表的长度或大小？ 在本教程中，我们将研究获取长度列表的不同方法。

## 使用内置的len()函数 (Using Built-in len() Function)

As we stated before len is a builtin function provided python by default. We can use this function just providing the list as an argument like below. The syntax is very simple we will just provide the list, array, tuple or dictionary type variable into the len() function as an array.

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,6,7,8,9,10,12,13,14,15]
len(number_list)

## 获取多维列表长度(Get Multi Dimension List Length)

In previous we have looked at the length of a single dimension list. But in real-world situations, there will be multi-dimension lists. We can also get the length of this list single dimension length just providing the index of the related sub-list like below. In this example, we want to get the length of the first sub-array.

name_list=[['ismail','elif'],'ahmet','ali',[7,8,9,10],1,2,3,['apple', 'banana', 'carrot' , 'melon' , 'tomato']]
len(name_list)
len(name_list[0])
len(name_list[7])

We provide the sublist element index as  which is ['ismail','elif'] and get the length of this sub-list as  2

## 使用For循环通过迭代每个元素来计算长度 (Count Length with For Loop By Iterating Each Element)

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 a list length or size by counting it one by one. Even we want to eliminate some elements in a list and do not count them. In this example, we can achieve this by using for loop with a list.

len()函数提供了一种非常方便，轻松且有效的方式来获取数组的长度或大小。 但是在某些情况下，我们可能希望通过一一计算列表的长度或大小。 甚至我们也想消除列表中的某些元素并且不计算它们。 在此示例中，我们可以通过for列表使用for循环来实现。

name_list=['ismail','ahmet','ali']
count=0
for element in name_list:
count=count+1
print(count)

name_list=[['ismail','elif'],'ahmet','ali',[7,8,9,10],1,2,3,['apple', 'banana', 'carrot' , 'melon' , 'tomato']]
count=0
for element in name_list:
count=count+1
print(count)

## 字典长度(Dictionary Length)

len() function is very useful where it can be used to get the length or size of different array types like the dictionary. We can use the same syntax to count dictionary key, value type elements. This will count 1 a single key and value pair.

len()函数在获取诸如字典之类的不同数组类型的长度或大小时非常有用。 我们可以使用相同的语法来计算字典键，值类型元素。 这将对1个键和值对进行计数。

name_surname={'ismail':'baydan','ahmet':'baydan','ali':'baydan'}

len(name_surname)

10-20 52

09-08 5万+
07-14 3万+
12-05 2914
03-28 2万+
03-01 645
08-29 244
04-23 3060
02-27 1万+
11-19 4777
12-19 1402
12-06 212
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客