Python | 在列表中指定索引處添加元素的程序

Given a list and we have to add an element at specified index in Python.

給定一個列表,我們必須在Python中的指定索引處添加一個元素。

list.appened() Method is used to append/add an element at the end of the list. But, if we want to add an element at specified index, we use insert() method. It takes 2 arguments, index and element.

list.appened()方法用于在列表末尾添加/添加元素。 但是,如果要在指定的索引處添加元素,則可以使用insert()方法。 它需要2個參數, indexelement

Syntax:

句法:

 list.insert(index, element)

Here,

這里,

  • list is the name of the list, in which we have to insert element at given index.

    list是列表的名稱,我們必須在其中給定index插入元素 。

  • index is the position, where we want to insert an element.

    index是我們要插入元素的位置。

  • element is an element/item to be inserted in the list.

    element是要在列表中插入的元素/項目。

Example:

例:

    list.insert(2, 100)
It will insert 100 at 2nd position in the list name ‘list’.

Program:

程序:

# Declaring a list
list = [10, 20, 30]
# printing elements
print (list)
# O/P will be: [10, 20, 30]
# inserting "ABC" at 1st index
list.insert (1, "ABC")
# printing
print (list)
# O/P will be: [10, 'ABC', 20, 30]
# inserting "PQR" at 3rd index
list.insert (3, "PQR")
# printing
print (list)
# O/P will be: [10, 'ABC', 20, 'PQR', 30]
# inserting 'XYZ' at 5th index
list.insert (5, "XYZ")
print (list)
# O/P will be: [10, 'ABC', 20, 'PQR', 30, 'XYZ']
# inserting 99 at second last index 
list.insert (len (list) -1, 99)
# printing
print (list)
# O/P will be: [10, 'ABC', 20, 'PQR', 30, 99, 'XYZ']

Output

輸出量

    [10, 20, 30]
[10, 'ABC', 20, 30]
[10, 'ABC', 20, 'PQR', 30]
[10, 'ABC', 20, 'PQR', 30, 'XYZ']
[10, 'ABC', 20, 'PQR', 30, 99, 'XYZ']

翻譯自: https://www.includehelp.com/python/add-an-element-at-specified-index-in-a-list.aspx

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/542413.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/542413.shtml
英文地址,請注明出處:http://en.pswp.cn/news/542413.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Linux九大哲學原理,Linux/Unix設計思想(全新闡釋開源哲學,Jon “maddog” Hall作序推薦)...

《Linux/Unix設計思想》第1章  Unix哲學:集思廣益的智慧    11.1  NIH綜合征    21.2  Unix的開發    21.3  Linux:一個人加上一百萬人的智慧    41.4  Unix哲學概述    5第2章  人類的一小步    92.1  準則1:小即是美  …

[Android] 開源View組件(一)

Material Design系列,自定義Behavior實現Android知乎首頁 仿今日頭條最強頂部導航指示器,支持6種模式 MagicIndicator系列之一 —— 使用MagicIndicator打造千變萬化的ViewPager指示器 Android 優雅的為RecyclerView添加HeaderView和FooterView Android …

sql server修改字段編碼格式_關于MySQL如何修改character_set_client的編碼問題

問題引入:我們經常會遇到一些向MySQL數據庫中插入中文,但是select出來的時候,卻發現是亂碼的情況。如我們向表a出入這樣一段記錄:iinsert into a values(‘你好helloworld你好’,’helloworld’);可能當你訪問它的時候&#xff0c…

通用apdu指令_8086微處理器中的通用指令格式

通用apdu指令Introduction: 介紹: In this article, we are going to discuss about the 6 general formats of instructions. 在本文中,我們將討論6種通用指令格式。 One byte instruction: 一字節指令 : This is only one byte long an…

Linux中斷不能進行任務調度,關中斷是否禁止任務調度?關中斷能作為互斥嗎?...

今天再看《嵌入式軟件系統教程》((美)西蒙 著,陳向群 等譯) ,里面講到關中斷會關了任務調度,作者沒說原因,我也不知道為什么,所以查了查網絡。在這個網址http://www.hqwic.com/bbs/topic.aspx?topicid11253上有一個討…

Win10系統怎樣讓打開圖片方式為照片查看器

打開注冊表編輯器之后,我們雙擊左側的目錄,依次打開HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft目錄,如下圖所示。 在Microsoft目錄下,我們需要找到Windows Photo Viewer\Capabilities\FileAssociations目錄項,直到看到該子…

python爬蟲開發 從入門到實戰 pdf 謝乾坤_python字符串和整數相互轉換的實例分享...

python 字符串和整數的轉換方法數字轉成字符串方法一:使用格式化字符串:tt322tem%d %tttem即為tt轉換成的字符串常用的格式化字符串:%d 整數%f%F 浮點數%e%E 科學計數%g%G e 和%f/%E 和%F 的簡寫%% 輸出%格式化操作符輔助指令符號 作用* 定義寬度或者小數…

c語言插入排序算法_插入排序算法,流程圖和C,C ++代碼

c語言插入排序算法In the last article, we discussed about the bubble sort with algorithm, flowchart and code. In this article, we are going to discuss about another basic sorting technique i.e. insertion sort. 在上一篇文章中,我們討論了用算法&…

EF使用CodeFirst方式生成數據庫技巧經驗

前言 EF已經發布很久了,也有越來越多的人在使用EF。如果你已經能夠非常熟練的使用EF的功能,那么就不需要看了。本文意在將自己使用EF的方式記錄下來備忘,也是為了給剛剛入門的同學一些指導。看完此文,你應該就學會以CodeFirst的方…

java jar包示例_Java包getImplementationVersion()方法和示例

java jar包示例包類的getImplementationVersion()方法 (Package Class getImplementationVersion() method) getImplementationVersion() method is available in java.lang package. getImplementationVersion()方法在java.lang包中可用。 getImplementationVersion() method …

c語言中字母的定義,c語言字符串定義與初始化 - 且聽風吟

字符串的兩種定義方式char數組char sa[] “hello world”;char指針char *sp “hello world”;這兩種方式都產生了一個”hello world”的字符串常量,字符串常量存儲在靜態存儲區中,靜態存儲區中的內容在程序運行的整個過程中都存在,而且只存儲一份。數組…

python計算兩字符串中的位置_python – 計算兩個字符串之間距離的算法

是否有任何字符串距離算法沒有考慮到單詞的順序?以下算法未提供所需結果(在該示例中,所需結果應為1):import jarojaro.jaro_winkler_metric(uMichael Jordan,uJordan Michael)>>>0.47import LevenshteinLevenshtein.ratio(Michael Jordan,Jorda…

php unset函數_PHP | 使用unset()函數從數組中刪除元素

php unset函數Given an array and we have to remove an element from the array. 給定一個數組,我們必須從數組中刪除一個元素。 unset()函數 (unset() function) To remove an element from an array, we can use a PHP library unset() function, it accepts th…

vi顯示行號

vi顯示行號 :set nu 帶行號查看,并不改變文件內容:set nonu 取消帶行號查看在每個用戶的主目錄下,都有一個 vi 的配置文件".vimrc"或".exrc"用戶可以編輯它,使這些設置在每次啟動 vi 時,都有效.例如,加入如下設置行:set nu 顯示行號…

對象過濾某個屬性 循環 php_37道PHP面試題(附答案)

1、什么事面向對象?主要特征是什么?面向對象是程序的一種設計方式,它利于提高程序的重用性,使程序結構更加清晰。主要特征:封裝、繼承、多態。2、SESSION 與 COOKIE的區別是什么,請從協議,產生的…

項響琴C語言書籍在線瀏覽,電子琴 c語言程序

實用#include unsigned char code table[]{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};unsigned char temp;unsigned char key;unsigned char i,j;unsigned char STH0;unsigned char STL0;unsigned int code tab[]{64021,64103,64260,…

Java File類boolean createNewFile()方法(帶示例)

文件類布爾型createNewFile() (File Class boolean createNewFile()) This method is available in package java.io.File.createNewFile(). 軟件包java.io.File.createNewFile()中提供了此方法。 This method is used to create a new file by using createNewFile() method a…

oracle ? SQL執行過程

1.sql執行過程1>解析(判斷對象是否存在,是否有權限查詢,語義解析,檢查緩存中是否有相同的SQL等等)2>優化(CBO確定優化模式,確定訪問路徑,聯接順序,過程中通過很多綜…

vue-video-player修改src就會報錯_4、修改入口點代碼

在riscv上電時,會進行CPU自檢,然后跳轉到bootloader處執行。bootloader設置好kernel的運行環境后,從硬盤加載kernel到內存,最后再跳轉到kernel入口地址。我們采用的bootloader為OpenSBI,被加載到0x80000000地址&#x…

數碼管超聲波c語言黑51,51單片機開發板-超聲波測距-數碼管顯示

《51單片機開發板-超聲波測距-數碼管顯示》由會員分享,可在線閱讀,更多相關《51單片機開發板-超聲波測距-數碼管顯示(16頁珍藏版)》請在人人文庫網上搜索。1、計算機技術系項目工作報告課程名稱單片機開發板設計與制作實訓班級學號姓名項目名稱超聲波測距…