Explanation of append Function Usage in python

  • 2021-08-17 00:32:51
  • OfStack

If you are doing statistical work in a region, you can use the list to help us. Enter Chinese characters or other characters, such as "01 stands for Han nationality". If there is a drop-down list when writing ethnic groups, you can type 01 and automatically recognize it as Han nationality. Lists are used when filling in large-scale data. In python, there are many times when lists are used. Do you know how to add new objects at the end of the list? Today, let's look at the append function in python under 1, which can add elements at the end of the list.

1. The append () function

Used to add a new object at the end of the list.

2. Grammar


list.append(obj)

3. Parameters

list: List object;
obj: The object added to the end of the list.

4. Return value

The append () function returns no value, but modifies the original list.

5. Use examples


#!/usr/bin/python
#Filename:append.py
a=[-1,3,'aa',85,90,'dasd']
a.append('add')
print a

Output


[-1, 3, 'aa', 85, 90, 'dasd', 'add']

Extension of knowledge points:

Generating multidimensional arrays with append:


import numpy as np
a=[] 
for i in range(5): 
  a.append([])
  for j in range(5): 
    a[i].append(i)

The results are as follows:


[[0, 0, 0, 0, 0],
 [1, 1, 1, 1, 1],
 [2, 2, 2, 2, 2],
 [3, 3, 3, 3, 3],
 [4, 4, 4, 4, 4]]

Related articles: