11 important operations of Python list

Jabami Mengzi 2020-11-08 16:37:42
important operations python list


The list is python Built in data structure in , It takes the form of a collection of different data in square brackets , Separate... With commas . Lists can be used to store the same data type or different data types .

The list is variable , That's why it's so common , In some cases, however , Variability requires special attention .

This paper introduces 11 about python Important operation of list , Help you master the list better .

1. Three ways to remove elements from a list

(1)del

del According to the index position , Delete an element , You can also set the index range to delete , You can also delete the entire list , no return value

Delete single element

a = [1, 2, 'x', 4, 5]
del(a[0])
print(f'a is {a}')

Output

a is [2, 'x', 4, 5]

Range delete element

a = [1, 2, 'x', 4, 5]
del (a[0:2])
print(f'a is {a}')

Output

a is ['x', 4, 5]

Delete the entire list

a = [1, 2, 'x', 4, 5]
del (a[0:5])
print(f'a is {a}')
# Delete the elements of the entire list by index range , An empty list will be output a is []

a = [1, 2, 'x', 4, 5]
del (a)
print(f'a is {a}')
# name 'a' is not defined no return value , Error report, no object found 

 

(2)remove

remove() Function to remove the first match of a value in the list , no return value

Example

a = [1, 2, 'x', 4, 5,]
a.remove('x')
print(f'a is {a}')

Output

a is [1, 2, 4, 5]

Be careful : Only the first matching value will be deleted , If there is a duplicate, it will not be deleted

a = [1, 2, 'x', 4, 5,'x']
a.remove('x')
print(f'a is {a}')

Output :

a is [1, 2, 4, 5, 'x']

(3)pop

pop() Function to remove an element from the list ( Default last element ), And returns the value of that element .

Example

a = [1, 2, 'x', 4, 5]
b = a.pop()
print(f'a is {a}')
print(f'b is {b}')

Output

a is [1, 2, 'x', 4]
b is 5

We can specify the index value of the deleted element , Cannot exceed the total length of the list .

a = [1, 2, 'x', 4, 5]
b = a.pop(0)
print(f'a is {a}')

Output

a is [2, 'x', 4, 5]

2. Add list , Expand , Insert

(1)append

append() Function is used to add a new object to the end of the list .

a = [1, 2]
a.append(3)
print(a)

Output

[1, 2, 3]

It can be any data type , The appended element remains the original structure type in the list

a = [1, 2]
b = ['x', 'y', 'z']
a.append(b)
print(a)

Output

[1, 2, ['x', 'y', 'z']]

list b Became a list a The third item in , If you want to create a list, it's a,b A combination of elements in a list a =(1, 2, 'x', 'y', 'z'), You need to use extend() Function or "+“.

(2)extend

extend() Function to append multiple values in another sequence at the end of the list at one time ( Extend the original list with the new list ).

a = [1, 2]
b = ['x', 'y', 'z']
a.extend(b)
print(a)

Output

[1, 2, 'x', 'y', 'z']

(3)insert

insert() Function is also used to add elements to a list . however , It allows you to specify the index of the new element . for example , We can add a new element at the beginning of the list

a = [1, 2, 3, 4, 5]
a.insert(0, 'a')
print(a)

Output

['a', 1, 2, 3, 4, 5]

insert() The first parameter in is the index , The second is the inserted object

3. use ”=“ Copy list

We can copy a list and create a new variable .

a = [1, 2, 3]
b = a
print(b)

however , New variable “ b” It's just pointing to “ a” Pointer to value . therefore ,a Any change in the will change b. Let's add a value to confirm .

a = [1, 2, 3]
b = a
a.append(4)
print(a)
print(b)

Output

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

4. Copy the list using the index

We can copy the list by selecting all the indexes of the list .

a = [1, 2, 3]
b = a[:]
print(b)

New list b Include and a The same value , But in different memory locations . If we change a,b Not affected .

a = [1, 2, 3]
b = a[:]
a.append(4)
print(a)
print(b)

Output

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

Use copy() Function copy list works the same way as index replication .

a = [1, 2, 3]
b = a.copy()
a.append(4)
print(a)
print(b)

Output

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

5. use sort() and sorted() Sort list .

sort and sorted Function can be used to sort a list .

  • sort(): Sort list , But it doesn't return anything .
  • sorted(): Returns a sorted copy of the list , But don't sort the original list .

sort()

a = [2, 0, 4, 3]
b = a.sort()
print(a)
print(b)

Output

[0, 2, 3, 4]
None

use sorted() Function does the same thing .

a = [2, 0, 4, 3]
b = sorted(a)
print(a)
print(b)

Output

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

6. Create nested lists by copying

When creating a nested list by copying , We need to pay attention to the changes in the elements . Any modification of the original list element changes the copied list .

a = [1, 2]
b = ['x', 'y']
c = [a, b]
a.append(3)
b.pop()
print(a)
print(b)
print(c)

list c By list a And list b form , We updated the list by adding and removing an element a And list b. therefore , list c Also updated .

Output

[1, 2, 3]
['x']
[[1, 2, 3], ['x']]

To be continued ...

版权声明
本文为[Jabami Mengzi]所创,转载请带上原文链接,感谢

  1. 利用Python爬虫获取招聘网站职位信息
  2. Using Python crawler to obtain job information of recruitment website
  3. Several highly rated Python libraries arrow, jsonpath, psutil and tenacity are recommended
  4. Python装饰器
  5. Python实现LDAP认证
  6. Python decorator
  7. Implementing LDAP authentication with Python
  8. Vscode configures Python development environment!
  9. In Python, how dare you say you can't log module? ️
  10. 我收藏的有关Python的电子书和资料
  11. python 中 lambda的一些tips
  12. python中字典的一些tips
  13. python 用生成器生成斐波那契数列
  14. python脚本转pyc踩了个坑。。。
  15. My collection of e-books and materials about Python
  16. Some tips of lambda in Python
  17. Some tips of dictionary in Python
  18. Using Python generator to generate Fibonacci sequence
  19. The conversion of Python script to PyC stepped on a pit...
  20. Python游戏开发,pygame模块,Python实现扫雷小游戏
  21. Python game development, pyGame module, python implementation of minesweeping games
  22. Python实用工具,email模块,Python实现邮件远程控制自己电脑
  23. Python utility, email module, python realizes mail remote control of its own computer
  24. 毫无头绪的自学Python,你可能连门槛都摸不到!【最佳学习路线】
  25. Python读取二进制文件代码方法解析
  26. Python字典的实现原理
  27. Without a clue, you may not even touch the threshold【 Best learning route]
  28. Parsing method of Python reading binary file code
  29. Implementation principle of Python dictionary
  30. You must know the function of pandas to parse JSON data - JSON_ normalize()
  31. Python实用案例,私人定制,Python自动化生成爱豆专属2021日历
  32. Python practical case, private customization, python automatic generation of Adu exclusive 2021 calendar
  33. 《Python实例》震惊了,用Python这么简单实现了聊天系统的脏话,广告检测
  34. "Python instance" was shocked and realized the dirty words and advertisement detection of the chat system in Python
  35. Convolutional neural network processing sequence for Python deep learning
  36. Python data structure and algorithm (1) -- enum type enum
  37. 超全大厂算法岗百问百答(推荐系统/机器学习/深度学习/C++/Spark/python)
  38. 【Python进阶】你真的明白NumPy中的ndarray吗?
  39. All questions and answers for algorithm posts of super large factories (recommended system / machine learning / deep learning / C + + / spark / Python)
  40. [advanced Python] do you really understand ndarray in numpy?
  41. 【Python进阶】Python进阶专栏栏主自述:不忘初心,砥砺前行
  42. [advanced Python] Python advanced column main readme: never forget the original intention and forge ahead
  43. python垃圾回收和缓存管理
  44. java调用Python程序
  45. java调用Python程序
  46. Python常用函数有哪些?Python基础入门课程
  47. Python garbage collection and cache management
  48. Java calling Python program
  49. Java calling Python program
  50. What functions are commonly used in Python? Introduction to Python Basics
  51. Python basic knowledge
  52. Anaconda5.2 安装 Python 库(MySQLdb)的方法
  53. Python实现对脑电数据情绪分析
  54. Anaconda 5.2 method of installing Python Library (mysqldb)
  55. Python implements emotion analysis of EEG data
  56. Master some advanced usage of Python in 30 seconds, which makes others envy it
  57. python爬取百度图片并对图片做一系列处理
  58. Python crawls Baidu pictures and does a series of processing on them
  59. python链接mysql数据库
  60. Python link MySQL database