# Dictionary derivation, list push

Keywords: Python

## List derivation and dictionary derivation of python

#### 1. List derivation

A list derivation is a conditional derivation used in conjunction with a loop to return a list, and the entire expression needs to be within [] because the return value is also a list

Requirement: multiply the even number from 0 to 20 by 10, the odd number by 100, and return all the calculations
Results. The sample code is as follows: (implemented by list derivation)
list2 = [x*10 if x%2 == 0 else x*100 for x in range(0,21) ]
print(list2)
print(type(list2))

result

[0, 100, 20, 300, 40, 500, 60, 700, 80, 900, 100, 1100, 120, 1300, 140, 1500, 160, 1700, 180, 1900, 200]
<class 'list'>

Maybe many children have questions. I know the functions that can be realized by the for loop. Why do I use this to deduce? The efficiency of list derivation is much higher than for loop. It may take only 0.0002 seconds for cpu to execute a print("Hello world") sentence. You may not feel the gap. What if you need to output 100 million Hello world times? Often the details feel success or failure!

#### 2. Dictionary derivation

In the late stage of crawler, when the cookie splits the string to pass the dictionary value, the dictionary derivation is often used, which is simple, convenient and practical.

• analysis
1. In string cookies, key is in front of '=', value is after '=', each ';' constitutes a key value pair; multiple key value pairs constitute a dictionary;
2. Split the string into lists according to ';';
3. According to the list obtained in the first step, each string will be split again according to '=' during traversal;
4. According to the split result of the second step, the first element of the list is the key, and the second element of the list is the value;
cookies = "anonymid=jy0ui55o-u6f6zd; depovince=GW; _r01_=1; JSESSIONID=abcMktGLRGjLtdhBk7OVw; " \
"_gid=GA1.2.201589596.1562980962; _c1=-100; first_login_flag=1; ln_uact=18323008898; " \