# This is what you should do to quickly create a list in Python

2022-01-30 13:58:31

# Background the problem

If , I want to create [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], Such a list , How to do ？

Of course , We can just write this list down

``````#  Write it directly into a list , It's a way
#  however , Not a good way
#  For example, to create from 1 To 100 List of words , We can't really write out all the elements
li = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Copy code ``````

# resolvent

We have many ways to solve such a problem

## Method 1 ： adopt list()

python There is a function called range(), Can generate a sequence . If we need 1-10 Such a sequence , Just use range(1, 11) That's all right. . This used to be python2 When ,range(1, 11) What you get is a list . But in python3 in , Due to performance optimization , It's no longer a list , But we can still get through list(), Turn it into a list

``````li = list(range(1, 11))
Copy code ``````

## Method 2 ： Through the loop

Add... Through a loop , Get a list

``````li = []
#  loop 1-10, Add it to the empty list prepared in advance
for i in range(1, 11):
li.append(i)
Copy code ``````

Of course , There are many ways to add loops

``````li = []
#  The effect is equivalent to append()
for i in range(1, 11):
li += [i]
Copy code ``````

## Method 3 ： Generated by list

Because the method of adding a list through a loop is more troublesome ( It means that it may be written in multiple lines ), In order to solve such a problem in one line , therefore ,python There is also a syntax called list generation , Can be on the same line , Solve such a problem .

``````#  Use only one line , We can solve such a problem
li = [i for i in range(1, 11)]
Copy code ``````

## List generator

List generator , It can also be called list derivation , Is a very convenient way to generate lists . In the previous demonstration , We've seen the benefits of writing it on one line .

however , There may be someone who has questions , Use list() It also used only one line , Why do you need a list generator ？

Of course , In a simple requirement , It's hard for us to directly realize the benefits of using list generation , But in complex requirements , We can find out , Using list generators is beneficial .

## A single cycle

If , We want to create [1, 4, 9, 16, 25, 36, 49, 64, 81, 100], Such a list , That is to say 1-10 The square of all numbers , How to do

obviously , For such a demand that is not difficult , We use list() It's not easy to solve

``````#  Traditional approach , Add... Through a loop
old_li = []
for i in range(1, 11):
old_li.append(i * i)

#  Using the new list derivation method , A line to solve
new_li = [i * i for i in range(1, 11)]
Copy code ``````

actually , We can see that , By means of circulation , And the method of writing directly in the list derivation , It's the same , The two can replace each other . in other words , If you don't use list derivation at all , No problem .

## Multiple cycles

such as , We're going to output a 99 multiplication table

``````li = ["%s * %s = %s" % (i, j, i * j) for i in range(1, 10) for j in range(1, 10)]
Copy code ``````

There are many cycles , Just write it directly behind , The above code is equivalent to

``````li = []
for i in range(1, 10):
for j in range(1, 10):
li.append("%s * %s = %s" % (i, j, i * j))
Copy code ``````

## if conditional

We have such a new demand , Maybe when we usually use the 99 multiplication table , It only needs 1 * 2 = 2 That's all right. , Unwanted 2 * 1 = 2, that , We can do that

``````#  Just after the original list generator , Add a if i <= j The judgment of the , To complete the task
li = ["%s * %s = %s" % (i, j, i * j) for i in range(1, 10) for j in range(1, 10) if i <= j]
Copy code ``````

This code is equivalent to

``````li = []
for i in range(1, 10):
for j in range(1, 10):
if i <= j:
li.append("%s * %s = %s" % (i, j, i * j))
Copy code ``````

# summary

List generation is a fast and concise way to write , If you can master it , that , You can easily create a list of what you need . however , List generation is not necessary , It at any time , Can be replaced by a loop . therefore , If you find it difficult for you to write a list generator , You can also directly select the cycle .