🎏 This is the 14th day of my participation in the Gwen Challenge. Check out the details: Gwen Challenge

0 x00 📢 preface

Python is a programming language that knows how not to interfere with your programming. It’s easy to learn and powerful enough to build Web applications and automate boring stuff.

This article is an introduction to lists of sequence types.

0 x01 list (list)

Lists are the built-in types of sequences and are one of the most commonly used Python data types.

Lists vs tuples

The main difference between lists and tuples is that lists can be modified, whereas tuples cannot. This means that lists are good for situations where you need to add elements halfway through, and tuples are good for situations where changing the sequence is prohibited for some reason. Lists can be used instead of tuples in almost all cases. You cannot use lists instead of tuples when they are used as dictionary keys because dictionary keys are not allowed to be modified.

Create a list of

List syntax is simple: separate comma-separated data items in square brackets [].

>>> [1.2.3] 
[1.2.3] 

>>> [1]  
[1]

# an empty list
>>> []
[]  
Copy the code

Create using list()

The built-in function list() converts any sequence to a list. Lists aren’t really functions, they’re types.

>>> list('Hello')
['H'.'e'.'l'.'l'.'o']
Copy the code

Type () Indicates the type

View the data type through the type() function.

>>> type([1.2.3]) 
<class 'list'>
Copy the code

Access list

You can use an Index to access an element in a list (to get the value of an element), or you can use slicing to access a set of elements in a tuple (to get a new sublist). Indexes can be negative. When using slice access, you can pass in three parameters: the first parameter indicates the start index, the second parameter indicates the end index, and the third parameter indicates the step size.

>>> numbers = [1.2.3.4.5.6.7.8.9.10]

>>> numbers[2]
3   
>>> numbers[-3] 
8   

>>> numbers[3:6] 
[4.5.6]
>>> numbers[0:1] 
[1]
>>> numbers[-3: -1]
[8.9]
>>> numbers[-3:]
[8.9.10]
>>>> numbers[:3]
[1.2.3]

# entire list
>>> numbers[:]
[1.2.3.4.5.6.7.8.9.10]

# step by step
>>> numbers[0:10:2]
[1.3.5.7.9]
>>> numbers[::4]
[1.5.9]
>>> numbers[10:0: -2]
[10.8.6.4.2]
Copy the code

Membership check

To check whether a particular value is included in the list, use the operator in.

>>> a = [1.2.3]
>>> 1 in a
True
>>> 4 not in a
True
Copy the code

Iteration

Use the for loop to iterate over any iterable (list).

>>> for i in [1.2.3] :. print(i)         
.
1
2
3
Copy the code

Modify the list

Index the element at a specific location and then assign.

>>> numbers = [1.2.3.4.5.6.7.8.9.10] 
>>> numbers[1] = 22
>>> numbers         
[1.22.3.4.5.6.7.8.9.10] 
Copy the code

Use the addition operator to concatenate tuples and add new elements.

>>> a = [1.2.3]
>>> a = a + [4.5.6]
>>> a
[1.2.3.4.5.6]     
Copy the code

When you multiply a list by a number n, the list is repeated n times to create a new list.

>>> a = [42]
>>> a = a * 10
>>> a
[42.42.42.42.42.42.42.42.42.42]

# None
>>> [None] * 10
[None.None.None.None.None.None.None.None.None.None]

# an empty list
>>> [] * 10
[] 

Copy the code

Delete the list

Use the DEL statement to delete list elements or lists. Garbage collection automatically destroys unused lists, so manual deletion by DEL is generally not required.

>>> a = [1.2.3]     
>>> del a[1]
>>> a
[1.3]

>>> del a
>>> a
Traceback (most recent call last):
  File "<stdin>", line 1.in <module>
NameError: name 'a' is not defined
Copy the code

0x02 List Comprehensions

List Comprehension provides an elegant way of generating lists that can replace a dozen or so lines of code with one line of code without loss of readability. List parsing is an application of Python’s iterative mechanism. Is a tool that converts one list (actually any iterable) into another list. During transformation, each element can be included in a new list under certain conditions, and some transformations can be made as needed.

The syntax is as follows: return a new list with the original contents unchanged.

newlist = [expression for item in可迭代if condition == True]
Copy the code

Find the fruit that contains the letter A.

>>> fruits = ["apple"."banana"."cherry"."mango"]
>>> newlist = []
>>> for x in fruits:
.  if "a" in x:
.    newlist.append(x)
. 
>>> print(newlist)
['apple'.'banana'.'mango']
Copy the code

Using list comprehensions, it takes only one line of code to make iterative judgments.

>>> newlist = [x for x in fruits if "a" in x]
>>> print(newlist)
['apple'.'banana'.'mango']
Copy the code