#Lift your hands to learn Python# list derivation and dictionary derivation

List derivation and dictionary derivation

Derivation is a very Python knowledge in Python. This blog will give you detailed answers to the technical knowledge related to list derivation and dictionary derivation.

List derivation

List derivation can quickly generate a specific list by using data types such as list, tuple, dictionary and collection.
The syntax format is as follows:

[expression for Iterative variable in Iteratable object [if Conditional expression]]

The if conditional expression is not required. After learning the list derivation, you can find that it is a variant of the for loop. For example, we have a requirement to change all elements in a list to twice the original value.

for loop writing

my_list = [1,2,3]
new_list = []
for i in my_list:
    new_list.append(i*2)

print(new_list)

List derivation

nn_list = [i*2 for i in my_list]
print(nn_list)

By comparison, the for loop statement is modified and a [], but it should be noted that the list derivation will eventually form a new list of the results obtained.
Let's look at the syntax of list derivation nn_list = [i*2 for i in my_list], the for keyword is followed by an ordinary loop. In the previous expression i*2, i is the variable in the for loop, that is, the expression can use the variable generated by the later iteration of the for loop. Understanding the derivation of this content list has 90% of the content, and the rest is the problem of proficiency.

After you include the if statement into the code and run it, you can also master the basic skills. The if statement is a judgment, and i is also the iterative variable generated by the previous loop.

nn_list = [i*2 for i in my_list if i>1]
print(nn_list)

These are general skills. The list derivation can support two-tier for loops, such as the following code:

nn_list = [(x,y) for x in range(3) for y in range(3) ]
print(nn_list)

Of course, if you want to encrypt your code (no one can understand your code), you can go on indefinitely. The list derivation does not limit the number of loop layers. Multi-layer loops are nested layer by layer. You can expand a three-layer list derivation to understand it

nn_list = [(x,y,z,m) for x in range(3) for y in range(3) for z in range(3) for m in range(3)]
print(nn_list)

Of course, in the multi-layer list derivation, the if statement is still supported, and the variables generated by all previous iterations can be used after the if, but it is not recommended to exceed 20%, which will greatly reduce the readability of your code.

Of course, if you want your code to be more difficult to read, the following is correct.

nn_list = [(x, y, z, m) for x in range(3) if x > 1 for y in range(3) if y > 1 for z in range(3) for m in range(3)]
print(nn_list)
nn_list = [(x, y, z, m) for x in range(3) for y in range(3) for z in range(3) for m in range(3) if x > 1 and y > 1]
print(nn_list)
nn_list = [(x, y, z, m) for x in range(3) for y in range(3) for z in range(3) for m in range(3) if x > 1 if y > 1]
print(nn_list)

Now you have a more intuitive concept of list derivation. The English corresponding to list derivation is list synthesis. In some places, write list parsing. Based on its final result, it is a syntax for creating lists, and it is very concise.

With two different writing methods, we must compare the efficiency. After testing, the small data range has little impact. When the number of cycles reaches tens of millions, there are some differences.

import time
def demo1():
    new_list = []
    for i in range(10000000):
        new_list.append(i*2)

def demo2():
    new_list = [i*2 for i in range(10000000)]
s_time = time.perf_counter()
demo2()
e_time = time.perf_counter()
print("Code run time:", e_time-s_time)

Operation results:

# for loop
 Code run time: 1.3431036140000001
# List derivation
 Code run time: 0.9749278849999999

In Python 3, list derivation has local scope. Variables and assignments inside the expression only work locally. Variables with the same name in the context of the expression can also be referenced normally, and local variables will not affect them. Therefore, it will not have the problem of variable leakage. For example, the following codes:

x = 6
my_var = [x*2 for x in range(3)]

print(my_var)
print(x)

List derivation also supports nesting
The reference code is as follows, only unexpected, nothing can't be done.

my_var = [y*4 for y in [x*2 for x in range(3)]]
print(my_var)

Dictionary derivation

With the concept of list derivation, dictionary derivation is very simple to learn. The syntax format is as follows:

{key:value for Iterative variable in Iteratable object [if Conditional expression]}

Just look at the case directly

my_dict = {key: value for key in range(3) for value in range(2)}
print(my_dict)

The results obtained are as follows:

{0: 1, 1: 1, 2: 1}

At this time, it should be noted that the key with the same name cannot appear in the dictionary. The second occurrence overwrites the first value, so the obtained value is 1.

The most common is the following code, which traverses an iteratable object with key value relationship.

my_tuple_list = [('name', 'eraser'), ('age', 18),('class', 'no1'), ('like', 'python')]
my_dict = {key: value for key, value in my_tuple_list}
print(my_dict)

Tuple derivation and set derivation

In fact, you should be able to guess that there are these two derivations in Python, and I believe you have mastered the syntax. However, although the syntax is similar, the results of tuple derivation are different, as follows.

my_tuple = (i for i in range(10))
print(my_tuple)

Results after running:

<generator object <genexpr> at 0x0000000001DE45E8>

The result generated by using tuple derivation is not a tuple, but a generator object. It should be noted that this writing method is called generator syntax in some places, not tuple derivation.

There is also a place to pay attention to in the set derivation. First look at the code:

my_set = {value for value in 'HelloWorld'}
print(my_set)

Because the collection is disordered and not repeated, the repeated elements will be automatically removed, and the display order is different each time, so it is easy to faint when using.

Summary of this blog

In this blog, we have learned list and dictionary derivation. After mastering and skillfully applying them, your Python skills have taken another step forward.

Keywords: Python Back-end

Added by errtu on Mon, 03 Jan 2022 08:21:42 +0200