What is list comprehension in python

You may be confused when you first see the list comprehension. They are a neat way to create and use lists. Understanding list comprehensions is useful, because you might see list comprehensions in other people's code. Let's understand the following list comprehension.

Derivation of Number List

Recalling what we have learned before, we can create a list containing the first 10 numbers as follows:

squares =[]for number inrange(1,11):
 new_square = number**2
 squares.append(new_square)for square in squares:print(square)

In the above code, we have implemented the function of creating a list of 10 numbers, squaring each number and storing them in a new array. The code is a bit verbose, we can omit the new_square parameter in the for loop to simplify the code. Using list comprehensions can further simplify the code, as shown below:

squares =[number**2for number inrange(1,11)]for square in squares:print(square)

The squaring operation and the process of generating a new list are condensed into one line of code. Are you dizzy, let's take a look at what happened to this line of code.

First we define a list named squares.

Next, look at the code in parentheses in the list:

for number inrange(1,11)

It creates a loop between 1-10 and stores each number in the variable number. Next, let's take a look at what operations are done on the number in each loop.

number**2

Each number is squared and the result is stored in a defined queue. We can read this line of code in the following language:

squares =[raise number to the second power,for each number in the range 1-10]

Other examples

The previous example is a square operation on a number. The following code is a multiplication operation on a number. Read the code carefully to experience the usage of a number list expression.

# Make an empty list that will hold the even numbers.
evens =[]
# Loop through the numbers 1-10, double each one, and add it to our list.for number inrange(1,11):
 evens.append(number*2)
# Show that our list is correct:for even in evens:print(even)

Comprehension of non-number lists

We can also use deductions in non-numeric lists. In the following example, we will create a non-number list, and then use the deduction to generate a new list. The source code without deduction is as follows:

# Consider some students.
students =['bernice','aaron','cody']
# Let's turn them into great students.
great_students =[]for student in students:
 great_students.append(student.title()+" the great!")
# Let's greet each great student.for great_student in great_students:print("Hello, "+ great_student)

We want to write the derivation shown below:

great_students = [add ‘the great’ to each student, for each student in the list of students]

The code is as follows:

# Consider some students.
students =['bernice','aaron','cody']
# Let's turn them into great students.
great_students =[student.title()+" the great!"for student in students]
# Let's greet each great student.for great_student in great_students:print("Hello, "+ great_student)

Extended exercise:

Generate a list of [[1,2,3],[4,5,6]....] The maximum value is within 100

First consider how we should write under normal circumstances

a =[]for x inrange(1,100,3):
 a.append([x,x+1,x+2])

And then convert it into a list comprehension

a =[[x,x+1,x+2]for x inrange(1,100,3)]

The above is the detailed content of what python list comprehension is. For more information about the meaning and usage of python list comprehension, please pay attention to other related articles on ZaLou.Cn!

Recommended Posts

What is list comprehension in python
What is introspection in python
What is object-oriented in python
What is an anonymous function in Python
What is a sequence table in Python
What is the function of adb in python
What is Python variable scope
Is there function overloading in python
What does rc1 mean in python
Python list comprehension operation example summary
What does def in python do
What is the use of Python
Python3 list
​What are the numbers in Python?
Everything in Python is an object
What does np do in python
Is there an algorithm in python language
What is the scope of python variables
What is the id function of python
What are web development frameworks in python
What system is good for self-study python
What is the prospect of python development
Is a number in python a variable type
What is the function body of python
Python is mainly used in which directions
Is there a helper function in python
Python randomly shuffles the elements in the list
Python 3.9 is here!
Functions in python
python list learning
[902] python list sort
Explain the list under Python multithreading in detail
What does the tab key in python mean
What is the difference between python and pycharm
Why python is popular
Python is short-crawling music
What is the difference between synchronous and asynchronous Python?
03. Operators in Python entry
What is the advantage of python over corporate language
Python is slowly fading
What conditions are needed for infinite loop in Python
What can Python do
Join function in Python
12. Network Programming in Python3
print statement in python
How to understand a list of numbers in python
Concurrent requests in Python
Install python in Ubuntu
Context management in Python
Arithmetic operators in python
Write gui in python
MongoDB usage in Python
Str string in Python
Computational Geometry in Python
What are the ways to open files in python
Concurrent requests in Python (part 2)
What are python class attributes
In-depth understanding of python list (LIST)
Subscripts of tuples in Python
Is python an interpreted language?
Talking about inheritance in Python