[Introduction to Python] How to get the size of a list with list size

Reference site: [Introduction to Python] How to get the size of a list with list size

[Introduction to Python] How to get the size of a list with list size

When working with lists in Python, you may want to know the size (length) of the list. In Python, the length of the list can be easily obtained by using a function. This time, I will explain how to get the size of a one-dimensional list and a multidimensional list.

table of contents 1 [Get the size of the list with the for statement](Get the size of the list with the ## for statement) 2 [Get the list size with the len function](## Get the list size with the len function) 3 [Get the size of the multidimensional list with the len function](## Get the size of the multidimensional list with the len function) 4 [Get the number of elements specified by the count function](## Get the number of elements specified by the count function)

Get the size of the list with a for statement

The most orthodox way to get the size of a list is with a for statement.

list1 = ['python', 'list', 'length']
length = 0

for x in list1:
    length += 1
     
print('The length of list1 is{}is'.format(length))

Execution result

list1 is 3 in length

Since the Python for statement can extract elements from the list one by one, prepare a counter variable (length in this example) and add 1 to the value of the counter variable each time to increase the size of the list. You can get it.

Get the size of the list with the len function

Earlier I explained how to get the size of the list by turning the for statement. You can get the size that way, but Python makes it easier to get the length. That's how to use the len function. The syntax of the len function is as follows.

variable= len(list)

The len function is a function that returns the length of the list passed as an argument. You can get the length of the list in just one line by using len.

list1 = ['python', 'list', 'length']
length = len(list1)

print('The length of list1 is{}is'.format(length))

Execution result

list1 is 3 in length

The source code is very refreshing. The method of using the for statement is not particularly complicated, but it is still cleaner to use the len function.

Get the size of a multidimensional list with the len function

Earlier we got the size of a one-dimensional list, but sometimes we want to get the size of a two-dimensional or larger multidimensional list. In the case of 1D, there was no problem using the for statement, but in the case of a multidimensional list, trying to retrieve it with just the for statement would be quite complicated.

To get the size of a multidimensional list, combine the len function with the for statement. It's a bit more complicated than 1D, but you can still get the size with a relatively small amount of code by using len.

This time, as an example, we will get the length of the following two-dimensional list.

list1 = [['python', 'list'], ['length', 'two', ' demension']]

First, let's apply the len function to this list normally.

list1 = [['python', 'list'], ['length', 'two', ' demension']]
length = len(list1)
 
print('The length of list1 is{}is'.format(length))

Execution result

list1 is 2 in length

This way the size is 2. This is because list1 is regarded as a one-dimensional list with two elements, ['python', ‘list’] and [‘length’, ‘two’, ‘demension’].

To get the size of the 2D list correctly, you need to get the size of each element and add them together. In other words, you can get the total size by passing each element to the len function and adding the results together.

list1 = [['python', 'list'], ['length', 'two', ' demension']]
length = 0

for x in list1:
    length += len(x)  #Add the lengths of the extracted elements

print('The length of list1 is{}is'.format(length))

Execution result

list1 is 5 in length

You have now successfully obtained the overall size. For 3D and above, you can basically get the overall size by using len in the for statement to get the size of each element.

Gets the number of elements specified by the count function

How to get the size of the list depends on the programming language, but some languages get it with a function called "count". In Python, the count function can get "the number of specific elements you specify" instead of the length of the list. Count is also a frequently used function, so it's a good idea to take this opportunity to learn how to use it. The syntax of count is as follows.

Variable = list.count (object)

The count function returns how many objects specified in the argument are in the list.

ist2 = [1, 3, 4, 5, 1, 2, 1]
count_one = list2.count(1)

print('1 in list2{}There are'.format(count_one))

Execution result

There are 3 1s in list2

By the way, if the specified object is not in the list, it will automatically return 0.

list2 = [1, 3, 4, 5, 1, 2, 1]
count_one = list2.count(6)

print('6 in list2{}There are'.format(count_one))

Execution result

There are 0 1s in list2

Recommended Posts

[Introduction to Python] How to get the size of a list with list size
How to get a list of files in the same directory with python
[Introduction to Python] How to get the index of data with a for statement
How to get the last (last) value in a list in Python
How to get a list of built-in exceptions in python
[Introduction to Python] How to get data with the listdir function
[Introduction to Python] How to write a character string with the format function
Python script to get a list of input examples for the AtCoder contest
Note: How to get the last day of the month with python (added the first day of the month)
How to display a list of installable versions with pyenv
How to get into the python development environment with Vagrant
How to get a list of links from a page from wikipedia
Get the source of the page to load infinitely with python.
How to connect the contents of a list into a string
How to check in Python if one of the elements of a list is in another list
How to count the number of occurrences of each element in the list in Python with weight
[python] Get a list of instance variables
Summary of how to use Python list
[Python] Get a list of folders only
How to get the vertex coordinates of a feature in ArcPy
[Python] How to get a value with a key other than value with Enum
From the introduction of JUMAN ++ to morphological analysis of Japanese with Python
How to send a request to the DMM (FANZA) API with python
Get a list of files in a folder with python without a path
[Python] A program that rotates the contents of the list to the left
PhytoMine-I tried to get the genetic information of plants with Python
How to pass the execution result of a shell command in a list in Python (non-blocking version)
After hitting the Qiita API with Python to get a list of articles for beginners, we will visit the god articles
[Algorithm x Python] How to use the list
Introduction to Python with Atom (on the way)
How to get dictionary type elements of Python 2.7
How to get the files in the [Python] folder
A python amateur tries to summarize the list ②
A layman wants to get started with Python
[Python] How to set the (client) window size inside the browser with Selenium
Receive a list of the results of parallel processing in Python with starmap
How to format a list of dictionaries (or instances) well in Python
I made a program to check the size of a file in Python
[Introduction to Python] What is the method of repeating with the continue statement?
[Python] Get the list of ExifTags names of Pillow library
[Chapter 2] Introduction to Python with 100 knocks of language processing
[Introduction to Udemy Python3 + Application] 27. How to use the dictionary
How to get mouse wheel verdict with Python curses
Python: Get a list of methods for an object
[Introduction to Python] <list> [edit: 2020/02/22]
[Introduction to Udemy Python3 + Application] 30. How to use the set
[Python] Summary of how to specify the color of the figure
[Introduction to Python] How to stop the loop using break?
[Python] How to rewrite the table style with python-pptx [python-pptx]
Get the operation status of JR West with Python
[Python] How to create a 2D histogram with Matplotlib
[Introduction to Python] Basic usage of the library matplotlib
[Python] How to output the list values in order
Get the stock price of a Japanese company with Python and make a graph
[Python] How to draw a scatter plot with Matplotlib
Python Note: The mystery of assigning a variable to a variable
[Chapter 4] Introduction to Python with 100 knocks of language processing
[Python] How to deal with the is instance error "is instance () arg 2 must be a type or tuple of types"
A memo on how to overcome the difficult problem of capturing FX with AI
A memo of misunderstanding when trying to load the entire self-made module with Python3
How to find the first element that matches your criteria in a Python list