Skip to content

Latest commit

 

History

History
22 lines (17 loc) · 515 Bytes

13 怎么将一个对象插入到列表中.md

File metadata and controls

22 lines (17 loc) · 515 Bytes

Python100

把Python知识点整理成100道习题,知识点来自两本书:Python基础教程(第3版)和流畅的Python,以后会定期加入更多的习题,大家帮忙点个赞哈,点赞越多,更新越快~

怎么将一个对象插入到列表中

解法1:用 insert 方法

>>> num = [1, 2, 4, 5]
>>> num.insert(2, 'three')
>>> num
[1, 2, 'three', 4, 5]

解法2:用切片的方式插入

>>> num = [1, 2, 4, 5]
>>> num[2:2] = ['three']
>>> num
[1, 2, 'three', 4, 5]