Python操作列表之List.insert()方法的使用

yipeiwu_com6年前Python基础

 insert()方法插入对象obj到列表的偏移量索引。
语法

以下是insert()方法的语法:

list.insert(index, obj)

参数

  •     index -- 这是要插入对象obj必要的索引
  •     obj -- 这是将被插入到给定的列表中的对象

返回值

此方法不返回任何值,但它插入指定的索引处的指定元素
例子

下面的例子显示了insert()方法的使用

#!/usr/bin/python

aList = [123, 'xyz', 'zara', 'abc']

aList.insert( 3, 2009)

print "Final List : ", aList

当我们运行上面的程序,它会产生以下结果:

Final List : [123, 'xyz', 'zara', 2009, 'abc']

相关文章

python-web根据元素属性进行定位的方法

1. 根据属性ID值进行定位 def test_find_element_by_id(self): # 定位搜索文本框 search_input = self.driver....

python局域网ip扫描示例分享

复制代码 代码如下:#!/usr/bin/python# -*- coding: utf-8 -*- from scapy.all import *from time import ct...

django url到views参数传递的实例

一、采用?a=1&b=2访问 修改views.py: views.py from django.shortcuts import render from django.http im...

详解用python生成随机数的几种方法

今天学习了用python生成仿真数据的一些基本方法和技巧,写成博客和大家分享一下。     本篇博客主要讲解如何从给定参数的的正态分布/均匀分布...

Python获取任意xml节点值的方法

本文实例讲述了Python获取任意xml节点值的方法。分享给大家供大家参考。具体实现方法如下: # -*- coding: utf-8 -*- import xml.dom.mini...