This is the sixth day of my participation in Gwen Challenge

“This article has participated in the weekend learning plan, click to see details”

Retrospective review

  • Python lists are one of the mutable data types (lists, dictionaries, and collections)

  • The data items in the list do not need to have the same data type. Such as [10, “aaa”, True, [20, 30], (5, 7), {1, 2, 3}]

  • You can add or delete a list

For example, to create a list object, the data items contain integer, string, tuple, dictionary type.

List = [10, "aaa", True, [20, 30], (5, 7), {1, 2, 3}]

DOCS

1. Create a list

There are four ways to create a list.

  • The square brackets

  • List () converts any iteration’s data to a list

  • Range ()/map() creates a list of integers

  • Derive to create a list

Next, let’s take a look at four different ways to create a list.

1.1 Create a vm using []

We can create it using the basic syntax of lists, []. Enclose comma-separated data items in square brackets

A print(a) = [10,20,39]Copy the code
1.2 Create using list()

The list() function is built into Python.

It converts any iterable data to a list type and returns the converted list.

When the argument is null, the list function creates an empty list

  • Creating an empty list
>>> a = list()
>>> a
[]

Copy the code
  • Convert strings to lists
>>> a = list("JUEJING")
>>> a
['J', 'U', 'E', 'J', 'I', 'N', 'G']
>>> 
Copy the code
  • Converts tuples to lists
> > > Tule = (10, 30) > > > a = list (Tule) > > > a [10, 20, 30] > > >Copy the code
  • Convert dictionaries to lists
>>> Dict = {"Name":"JUEJING"}
>>> a = list(Dict)
>>> a
['Name']
Copy the code

Note: When a dictionary is converted to a list, the dictionary values are dropped and only the keys of the dictionary are converted to a list.

If you want to convert all the dictionary values to a list, consider using the dictionary method dict.values().

  • Convert a collection to a list
> > > Set = {1, 2, "JUEJING"} > > > a = list (Set) > > > a [1, 2, 'JUEJING]Copy the code
1.3 Use range()/map() to generate a list

The syntax format of range() is:

range([start],end,[step])
Copy the code

Start parameter: Optional, indicates the start number. The default value is 0

End parameter: Specifies the end number. This parameter is mandatory.

Step parameter: Indicates the step size. The default value is 1

Range () in Python3 returns a range() object, not a list. We need to convert it to a list object using the list() method.

> > > list (range (3,10,2)) (3, 5, 7, 9) > > > list (range (15, 3, 1)) [11, 12, 13, 14, 15, 10, 9, 8, 7, 6, 5, 4] >>> list(range(3,-10,-1)) [3, 2, 1, 0, -1, -2, -3, -4, -5, -6, -7, -8, -9] >>>Copy the code

Map () syntax format:

The map (function, iterable)Copy the code

Function argument: function

Iterable argument: — One or more sequences

Map () maps the specified sequence based on the provided function.

The first argument function calls the function function with each element in the argument sequence, returning a new list of values returned by each function function.

>>> List (map(int,[1,2,3,4]) [1,2,3,4] >>>Copy the code

Note:

  1. Arguments must be iterable sequence objects. The arguments to the list function must be iterable. Python reported an error when a non-iterable object was selected as an argument
>>> list(10)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
>>> 
Copy the code
  1. Meaningless conversions – To convert a list to a list.
1.4 Use derivations to generate lists

We can use the control statement for loop, if statement to generate the list.

>>> a = [x*2 for x in range(5)] 8] >>> >>> a = [x*2 for x in range(100) if x%9==0] # 198] > > >Copy the code

2. Add elements to the list

There are five ways to add elements to a list

When the list elements increase, the list automatically performs memory management, greatly reducing the burden of the program, but this feature involves a large number of list elements moving, which is inefficient. We usually add elements at the end of the list unless necessary, which makes the list much more efficient.

2.1 Using append()

Modifying list objects in place is the fastest way to add new elements to a real list tail, and is highly recommended.

> > > a = [1, 2] > > > a.a ppend (10) > > > a [1, 2, 10] > > >Copy the code
2.2 Use insert()

Use the insert() method to insert the specified element into any specified position on the list object. This will cause all elements behind the insertion position to move, which will affect the processing speed. Avoid using this method when a large number of elements are involved.

> > > a = [1, 2] > > > Anderson nsert (1, 7) > > > a [1, 7, 2] > > >Copy the code
2.3 Use extend()

Adding all the elements of the target list to the end of the list is an in-place operation and does not create a new list object.

>>> a = [1,2]
>>> a.extend([6])
>>> 

Copy the code

2.4 Use ➕ to connect

Using the plus sign to join does not really add elements to the tail, but creates a new list object, copying the elements of the original list and the elements of the new list into the new list object. This involves a lot of copying and is not recommended for manipulating large numbers of elements

> > > a = [1, 2] > > > a + [5] [1, 2, 5] > > > id 4479245520 (a) > > > id + [5] (a) 4479245680Copy the code

2.5 Use multiplier extension

Using multiplication to extend the list, a new list is generated, with new list elements repeated multiple times over the original list elements

> > > a = [1, 2] > > > b = a * 3 > > > id (a [0]) 4476513184 > > > id (b [0]) 4476513184 > > > id [2] (b) 4476513184 > > >Copy the code

3. Delete list elements

3.1 Use the pop() function

Pop () removes and returns the element at the specified location, or, if not specified, the element at the end of the list is deleted by default

a.pop([index=-1])
Copy the code

Parameters:

Obj — Optional argument to remove the index value of the list element, which cannot exceed the total length of the list. The default is index=-1. Remove the last list value.

>>> a = [1,2,6] >>> a.pop() 6 >>Copy the code
3.2 Using the remove() function

The remove() function is used to remove the first match of a value in a list.

a.remove(obj)
Copy the code

Parameters:

Obj — Object to be removed from the list.

>>> a = [1,2,6]
>>> a.remove(2)
>>> a
[1, 6]
>>> 
Copy the code
3.3 Using del()

Deletes the specified element of the list.

>>> A = [1,2,6] >>> Del A [1] >>> A [1] >>>Copy the code

Note: index 3 moves forward to index 2 after deleting the position in index 2

4. List slices

Slicing is a Python sequence and its important operations, applicable to lists, tuples, strings, and so on.

The slice operation allows us to quickly extract or modify sublists. The standard format is:

[start:end:step]
Copy the code

Note: The second colon can be omitted when the step size is omitted.

> > > a =,2,6 [1] > > > a [1] [2, 6] > > > a [: : - 1] [6, 2, 1] > > > a =,2,6 [1] > > > a [:] [1, 2, 6] > > > a [1] [2, 6] >>> a[:2] [1, 2] >>> a[1:2] [2] >>> a[0:2:1] [1, 2] >>>Copy the code

Other operations (three numbers are negative) :

>>> a[::-1]
[6, 2, 1]
>>> a[-1::]
[6]
>>> a[-3:-2]
[1]
Copy the code

Okay, that’s it for this installment, and the next installment introduces sister tuples of lists.

Welcome to the comments section, correction, I am learning Python circle of friends cute, see you next time!