python insert

list.insert()方法 (list.insert() Method)

insert() is an inbuilt method in python, which is used to add an element /item at specified index to the list.

insert()是python中的内置方法,用于将指定索引处的元素/ item添加到列表中。

insert() is able to add the element where we want i.e. position in the list, which we was not able to do the same in list.append() Method.

insert()能够将元素添加到我们想要的位置,即列表中的位置,而list.append()方法无法做到这一点 。

Syntax:

句法:

    list.insert(index, element)

Here,

这里,

  • list is the name of the list.

    list是列表的名称。

  • index is the valid value of the position/index.

    index是头寸/索引的有效值。

  • element is an item/element to be inserted.

    元素是要插入的项目/元素。

Return type: Method insert() does not return any value, it just modifies the existing list.

返回类型:方法insert()不返回任何值,它仅修改现有列表。

Program:

程序:

# Python program to demonstrate
# an example of list.insert() method
# list
cities = ['New Delhi', 'Mumbai']
# print the list
print "cities are: ",cities
# insert 'Bangalore' at 0th index
index = 0
cities.insert(index,'Bangalore')
# insert 'Chennai' at 2nd index
index = 2
cities.insert(index, 'Chennai')
# print the updated list
print "cities are: ",cities

Output

输出量

    cities are:  ['New Delhi', 'Mumbai']cities are:  ['Bangalore', 'New Delhi', 'Chennai', 'Mumbai']

Explanation:

说明:

  • Initially there were two elements in the list ['New Delhi', 'Mumbai'].

    最初,列表中有两个元素['New Delhi','Mumbai']

  • Then, we added two more city names (two more elements) - 1) 'Bangalore' at 0th index and 2) 'Chennai' at 2nd index after adding 'Bangalore' at 0th index, by using following python statements:

    :在0在第二索引处0添加“班加罗尔” 索引,通过使用以下Python语句后1)“班加罗尔” 索引和2)“奈” -然后,我们增加了两个城市名称(两个元件)

# insert 'Bangalore' at 0th index
index = 0
cities.insert(index, 'Bangalore')
# insert 'Chennai' at 2nd index
index = 2
cities.insert(index, 'Chennai')

  • After inserting 'Bangalore' at 0th index, the list will be ['Bangalore', 'New Delhi', 'Chennai'].

    0 索引处插入“班加罗尔”后,列表将为['Bangalore','New Delhi','Chennai']

  • And, then we added 'Chennai' at 2nd index. After inserting 'Chennai' at 2nd index. The list will be ['Bangalore', New Delhi', 'Chennai', 'Mumbai'].

    然后,我们在第二个索引处添加了“ Chennai” 。 在第二个索引处插入“ Chennai”之后。 该列表将是['Bangalore',New Delhi','Chennai','Mumbai']

  • So, it is at our hand, what we have to do? If our requirement is to add an element at the end of the list - use list.append() Method, or if our requirement is to add an element at any index (any specified position) - use list.insert() Method.

    那么,这在我们眼前,我们该怎么办? 如果我们的要求是在列表的末尾添加一个元素-使用list.append()方法 ,或者我们的要求是在任何索引(任何指定位置)添加一个元素-使用list.insert()方法。

    翻译自: https://www.includehelp.com/python/list-insert-method-with-example.aspx

    python insert

python insert_Python列表| 带示例的insert()方法相关推荐

  1. python列表怎么转成数字,Python中列表元素转为数字的方法分析

    本文实例讲述了Python中列表元素转为数字的方法.分享给大家供大家参考,具体如下: 有一个数字字符的列表: numbers = ['1', '5', '10', '8'] 想要把每个元素转换为数字: ...

  2. python列表和元组的应用,Python中列表和元组的使用方法和区别

    一.二者区别 列表: 1.可以增加列表内容 append 2.可以统计某个列表段在整个列表中出现的次数 count 3.可以插入一个字符串,并把整个字符串的每个字母拆分当作一个列表段追加到列表当中 e ...

  3. Python对列表去重的4种方法

    Python对列表去重的4种方法 列表 去重 开发中对数组.列表去重是非常常见的需求,对一个list中的id进行去重,有下面几种方法,前面两种方法不能保证顺序, 后面两种方法可以保持原来的顺序. 下面 ...

  4. python关于列表去重和删除的方法

    python关于列表等操作@TOC 列表 1.去重的两种简单方法 2. 删除指定位置后的元素 list1 = ['统一社会信用代码:', '91310105572715984J', '企业名称:', ...

  5. python颜色列表代码seaborn_Python seaborn.set_color_codes方法代码示例

    # 需要导入模块: import seaborn [as 别名] # 或者: from seaborn import set_color_codes [as 别名] def generate_clus ...

  6. Python:列表去重的两种方法

    方法一:使用 set() 方法进行去重 # 一.准备一个带有重复数据的列表 list_1 = [1,2,3,1,5,6,2]# 二.使用 set() 方法进行去重处理 #[注:仅用 set() 方法处 ...

  7. python list列表排序的两种方法及实例讲解

    对List进行排序,Python提供了两个方法 方法1.用List的内建函数list.sort进行排序 list.sort(func=None, key=None, reverse=False) Py ...

  8. 【Python】列表类型操作函数和方法

  9. python 多重列表去重_Python对列表去重的多种方法(四种方法)

    Python对列表去重的多种方法(四种方法) 无聊统计了下列表去重到底有多少种方法.下面小编给大家总结一下,具体内容详情如下: 开发中对数组.列表去重是非常常见的需求,对一个list中的id进行去重, ...

最新文章

  1. 牛客假日团队赛1 B
  2. Python处理mat文件的三种方式
  3. python 立方体切割块数_blender python通过三角函数调整立方体Location排成圆
  4. robodk导出html错误,在优傲机器人示教器上调试RoboDK机器人程序
  5. Spring Security OAuth2源码解析(三)——单点登录。
  6. 体验 PHP under .NET Core
  7. 中海达gps软件wince_应用|无人机航测15分钟能做啥?中海达PPK告诉你答案
  8. nginx 中location和root,你确定真的明白他们关系?
  9. 小米10至尊纪念版今日开卖:120hz高刷+120W秒充
  10. onclick控制元素显示与隐藏时,点击第一次无反应的原因
  11. 三维视觉和三维点云基础概念
  12. Mac如何读写NTFS硬盘,NTFSTool让Mac也可以轻松读写NTFS硬盘
  13. 2021年前端关注的8个技术趋势
  14. Git(码云)配置精干版
  15. uafxcwd.lib(dllmodul.obj) : error LNK2005: _DllMain@12 already defined in main.obj
  16. 抖音将来会做电商小程序
  17. 《ffmpeg basics》中文版 -- 16.数字音频
  18. 记录一下http发起请求XHR状态显示(错误:已阻止)的原因
  19. Mac 上Grapher基础入门教程
  20. 【遇见大咖】测试界:你的圈子在哪?

热门文章

  1. 在linux上面找一个脚本,30个Linux Shell脚本经典案例
  2. 双表查询java代码_多表增删改查
  3. 获取html内标题,通过html敏捷包获取标题标签
  4. argparse:shell向Python中传参数
  5. ES备份工具elasticdump
  6. 配置文件bashrc与profile的区别
  7. ubuntu实现简单的划词工具
  8. Ajax_Apache访问资源文件的权限配置、资源存放路径配置、配置虚拟主机、动态网站静态网站区别...
  9. Apache-Flink深度解析-DataStream-Connectors之Kafka
  10. vue服务端渲染添加缓存