{"id":3408,"date":"2021-04-22T17:38:01","date_gmt":"2021-04-22T12:08:01","guid":{"rendered":"https:\/\/python-programs.com\/?p=3408"},"modified":"2021-11-22T18:39:21","modified_gmt":"2021-11-22T13:09:21","slug":"python-programming-list-methods","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-programming-list-methods\/","title":{"rendered":"Python Programming \u2013 List Methods"},"content":{"rendered":"

In this Page, We are Providing Python Programming \u2013 List Methods. Students can visit for more Detail and Explanation of Python Handwritten Notes<\/a>\u00a0Pdf.<\/p>\n

Python Programming \u2013 List Methods<\/h2>\n

List methods<\/strong><\/p>\n

Below are the methods of list-objects.<\/p>\n

list . append ( x )
\nAdd an item to the end of the list. It is same as list [ len ( list ) : len ( list ) ] = [ x ] .<\/p>\n

>>> a= [ 66 . 25 , 333 , 333 , 1 , 1234 . 5 ]\r\n>>> a . append ( 45 )\r\n>>> a\r\n[ 66 . 25 , 333 , 333 , 1 , 1234 . 5 , 45 ] \r\n>>> a= [ 66 . 25 , 333 , 333 , 1 , 1234 . 5 ]\r\n>>> a [ len ( a ) : len ( a ) ]=[ 45 ]\r\n>>> a\r\n[ 66 . 25 , 333 , 333 , 1 , 1234 . 5 , 45 ]<\/pre>\n

list . extend ( L )
\nExtend a list by appending all the items of a given list. It is same as list [ len ( list ) : len ( list ) ] =L.<\/p>\n

>>> a= [ 66 . 25 , 333 , 333 , 1 , 1234 . 5 ]\r\n>>> b= [ 7 . 3 , 6 . 8 ]\r\n>>> a . extend ( b )\r\n>>> a\r\n[ 66 . 25 , 333 , 333 , 1 , 1234 . 5 , 7 . 3 , 6 . 8 ]\r\n>>> a= [ 66 . 25 , 333 , 333 , 1 , 1234 . 5 ]\r\n>>> b= [ 7 . 3 , 6 . 8 ]\r\n>>> a [ len ( a ) : len ( a ) ]=b \r\n>>> a\r\n[ 66 . 25 , 333 , 333 , 1 , 1234 . 5 , 7 . 3 , 6 . 8 ]<\/pre>\n

list.insert ( i , x )
\nInsert an item at a given position in the list. The first argument i is the index before which an item x need to be inserted. It is same as list [i : i] = [x].<\/p>\n

>>> a= [ 66 . 25 , 333 , 333 , 1 , 1234 . 5 ]\r\n>>> a . insert ( 2 , 5 . 7 )\r\n>>> a\r\n[ 66 . 25, 333 , 5 . 7 , 333 , 1 , 1234 . 5 ]\r\n>>> a= [ 66 . 25 , 333 , 333 , 1 , 1234 . 5 ]\r\n>>> a [ 2 : 2 ] = [ 5 . 7 ]\r\n>>> a\r\n[ 66 . 25 , 333 , 5 . 7 , 333 , 1 , 1234 . 5 ]<\/pre>\n

list.index ( x [ , i [ , j ] ] )
\nReturn the index in the list of the first occurrence of item x. In other words, it returns the smallest index k such that list [k]==x and i<=k<j. A ValueError exception is raised in absence of item x.<\/p>\n

>>> a= [ 66 . 25 , 333 , 333 , 1 , 1234 . 5 ]\r\n>>> a . index ( 333 )\r\n1<\/pre>\n

list.remove ( x )
\nRemove the first item from the list whose value is x. An error (ValueError exception) occur in absence of item x. It is same as del list [list. index (x) ].<\/p>\n

>>> a= [ 66 . 25 , 333 , 333 , 1 , 1234 . 5 ]\r\n>>> a . remove ( 333 )\r\n>>> a\r\n[ 66 . 25 , 333 , 1 , 1234 . 5 ]\r\n>>> a= [ 66 . 25 , 333 , 333 , 1 , 1234 . 5 ]\r\n>>> del a [ a . index ( 333 ) ] \r\n>>> a\r\n[ 66 . 25 , 333 , 1 , 1234 . 5 ]<\/pre>\n

list . pop ( [ i ] )
\nRemove the item at the given position i in the list, and return it. If no index is specified (defaults to -1), pop () removes and returns the last item in the list.<\/p>\n

>>> a= [ 66 . 25 , 333 , 333 , 1 , 1234 . 5 ]\r\n>>> a . pop ( 3 )\r\n1\r\n>>> a\r\n[ 66 . 25 , 333 , 333 , 1234 . 5 ]\r\n>>> a . pop ( )\r\n1234 . 5 \r\n>>> a\r\n[ 66 . 25 , 333 , 333 ]<\/pre>\n

list.count ( x )
\nReturn the number of times item x appears in the list.<\/p>\n

>>> a= [ 66 . 25 , 333 , 333 , 1 , 1234 . 5 ]\r\n>>> a . count ( 333 )\r\n2<\/pre>\n

list.reverse ( )
\nReverse the element’s position in the list; no new list is returned. It is same as list=list [: : -1 ].<\/p>\n

>>> a= [ 66 . 25 , 333 , 333 , 1 , 1234 . 5 ]\r\n>>> a . reverse ( )\r\n>>> a \r\n[ 1234 . 5 , 1 , 333 , 333 , 66 . 25 ] \r\n>>> a= [ 66 . 25 , 333 , 333 , 1 , 1234 . 5 ]\r\n>>> a=a [ : : -1 ] \r\n[ 1234 . 5 , 1 , 333 , 333 , 66 . 25 ]<\/pre>\n

list.sort ( [ cmp [ , key [ , reverse ] ] ] )
\nSort the items of the list; no new list is returned. The optional arguments have same meaning as given in sorted () built-in function.<\/p>\n

>>> a= [ 66 . 25 , 333 , 333 , 1 , 1234 . 5 ]\r\n>>> a . sort ( ) \r\n>>> a\r\n[ 1 , 66 . 25 , 333 , 333 , 1234 . 5 ]\r\n>>> a= [ 66 . 25 , 333 , ' abc ' , 333 , 1 , ' ab ' , 1234 . 5 ] \r\n>>> a . sort ( )\r\n>>> a\r\n[ 1 , 66 . 25 , 333 , 333 , 1234 . 5 , ' ab ' , ' abc ' ]\r\n>>> a= [ 66 . 25 , 333 , ' abc ' , 333 , 1 , ' ab ' , 1234 . 5 ]\r\n>>> a . sort ( reverse=True )\r\n>>> a\r\n[ ' abc ' , ' ab ' , 1234 . 5 , 333 , 333 , 66 . 25 , 1 ]<\/pre>\n

Using list as Stack<\/strong><\/p>\n

The list methods make it very easy to use a list as a stack, where the last element added is the first element retrieved (“last-in, first-out” approach). To add an item to the top of the stack, use append (). To retrieve an item from the top of the stack, use pop ( ) without an explicit index. For example:<\/p>\n

>>> stack= [ 3 , 4 , 5 ]\r\n>>> stack . append ( 6 )\r\n>>> stack . append ( 7 )\r\n>>> stack\r\n[ 3 , 4 , 5 , 6 , 7 ]\r\n>>> stack . pop ( )\r\n7\r\n>>> stack [ 3 , 4 , 5 , 6 ]\r\n>>> stack . pop ( )\r\n6\r\n>>> stack . pop ( )\r\n5\r\n>>> stack \r\n[ 3 , 4 ]<\/pre>\n

Using list as queue<\/strong><\/p>\n

It is also possible to use a list as a queue, where the first element added is the first element retrieved (“first-in, first-out”); however, list is not efficient for this purpose. While appending and popping of elements from the end of list are fast, doing inserting and popping from the beginning of a list is slow (because all of the other elements have to be shifted by one).<\/p>\n

To implement a queue, use collections. deque which was designed to have fast appends and pops from both ends. For example:<\/p>\n

>>> from collections import deque\r\n>>> queue=deque ( [ \" Eric \" , \" John \" , \" Michael \" ] )\r\n>>> queue . append ( \" Terry \" )\r\n>>> queue . append ( \" Graham \" )\r\n>>> queue \r\ndeque ( [ ' Eric ' , ' John ' , ' Michael ' , ' Terry ' , ' Graham ' ] ) \r\n>>> queue . popleft ( )\r\n' Eric '\r\n>>> queue . popleft ( ) \r\n' John '\r\n>>> queue\r\ndeque ( [ ' Michael ' , ' Terry ' , ' Graham ' ] )<\/pre>\n","protected":false},"excerpt":{"rendered":"

In this Page, We are Providing Python Programming \u2013 List Methods. Students can visit for more Detail and Explanation of Python Handwritten Notes\u00a0Pdf. Python Programming \u2013 List Methods List methods Below are the methods of list-objects. list . append ( x ) Add an item to the end of the list. It is same as …<\/p>\n

Python Programming \u2013 List Methods<\/span> Read More »<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[5],"tags":[],"yoast_head":"\nPython Programming \u2013 List Methods - Python Programs<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/python-programs.com\/python-programming-list-methods\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Programming \u2013 List Methods - Python Programs\" \/>\n<meta property=\"og:description\" content=\"In this Page, We are Providing Python Programming \u2013 List Methods. Students can visit for more Detail and Explanation of Python Handwritten Notes\u00a0Pdf. Python Programming \u2013 List Methods List methods Below are the methods of list-objects. list . append ( x ) Add an item to the end of the list. It is same as … Python Programming \u2013 List Methods Read More »\" \/>\n<meta property=\"og:url\" content=\"https:\/\/python-programs.com\/python-programming-list-methods\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Programs\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/btechgeeks\" \/>\n<meta property=\"article:published_time\" content=\"2021-04-22T12:08:01+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-11-22T13:09:21+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:site\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prasanna\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/python-programs.com\/#organization\",\"name\":\"BTech Geeks\",\"url\":\"https:\/\/python-programs.com\/\",\"sameAs\":[\"https:\/\/www.instagram.com\/btechgeeks\/\",\"https:\/\/www.linkedin.com\/in\/btechgeeks\",\"https:\/\/in.pinterest.com\/btechgeek\/\",\"https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg\",\"https:\/\/www.facebook.com\/btechgeeks\",\"https:\/\/twitter.com\/btech_geeks\"],\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"contentUrl\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"width\":350,\"height\":70,\"caption\":\"BTech Geeks\"},\"image\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/python-programs.com\/#website\",\"url\":\"https:\/\/python-programs.com\/\",\"name\":\"Python Programs\",\"description\":\"Python Programs with Examples, How To Guides on Python\",\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/python-programs.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/python-programs.com\/python-programming-list-methods\/#webpage\",\"url\":\"https:\/\/python-programs.com\/python-programming-list-methods\/\",\"name\":\"Python Programming \u2013 List Methods - Python Programs\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/#website\"},\"datePublished\":\"2021-04-22T12:08:01+00:00\",\"dateModified\":\"2021-11-22T13:09:21+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/python-programs.com\/python-programming-list-methods\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/python-programs.com\/python-programming-list-methods\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/python-programs.com\/python-programming-list-methods\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/python-programs.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Programming \u2013 List Methods\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/python-programs.com\/python-programming-list-methods\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/python-programming-list-methods\/#webpage\"},\"author\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb\"},\"headline\":\"Python Programming \u2013 List Methods\",\"datePublished\":\"2021-04-22T12:08:01+00:00\",\"dateModified\":\"2021-11-22T13:09:21+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/python-programs.com\/python-programming-list-methods\/#webpage\"},\"wordCount\":457,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/python-programs.com\/python-programming-list-methods\/#respond\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb\",\"name\":\"Prasanna\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/174540ad43736c7d1a4c4f83c775e74d?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/174540ad43736c7d1a4c4f83c775e74d?s=96&d=mm&r=g\",\"caption\":\"Prasanna\"},\"url\":\"https:\/\/python-programs.com\/author\/prasanna\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Programming \u2013 List Methods - Python Programs","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/python-programs.com\/python-programming-list-methods\/","og_locale":"en_US","og_type":"article","og_title":"Python Programming \u2013 List Methods - Python Programs","og_description":"In this Page, We are Providing Python Programming \u2013 List Methods. Students can visit for more Detail and Explanation of Python Handwritten Notes\u00a0Pdf. Python Programming \u2013 List Methods List methods Below are the methods of list-objects. list . append ( x ) Add an item to the end of the list. It is same as … Python Programming \u2013 List Methods Read More »","og_url":"https:\/\/python-programs.com\/python-programming-list-methods\/","og_site_name":"Python Programs","article_publisher":"https:\/\/www.facebook.com\/btechgeeks","article_published_time":"2021-04-22T12:08:01+00:00","article_modified_time":"2021-11-22T13:09:21+00:00","twitter_card":"summary_large_image","twitter_creator":"@btech_geeks","twitter_site":"@btech_geeks","twitter_misc":{"Written by":"Prasanna","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/python-programs.com\/#organization","name":"BTech Geeks","url":"https:\/\/python-programs.com\/","sameAs":["https:\/\/www.instagram.com\/btechgeeks\/","https:\/\/www.linkedin.com\/in\/btechgeeks","https:\/\/in.pinterest.com\/btechgeek\/","https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg","https:\/\/www.facebook.com\/btechgeeks","https:\/\/twitter.com\/btech_geeks"],"logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/","url":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","contentUrl":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","width":350,"height":70,"caption":"BTech Geeks"},"image":{"@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/"}},{"@type":"WebSite","@id":"https:\/\/python-programs.com\/#website","url":"https:\/\/python-programs.com\/","name":"Python Programs","description":"Python Programs with Examples, How To Guides on Python","publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/python-programs.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/python-programs.com\/python-programming-list-methods\/#webpage","url":"https:\/\/python-programs.com\/python-programming-list-methods\/","name":"Python Programming \u2013 List Methods - Python Programs","isPartOf":{"@id":"https:\/\/python-programs.com\/#website"},"datePublished":"2021-04-22T12:08:01+00:00","dateModified":"2021-11-22T13:09:21+00:00","breadcrumb":{"@id":"https:\/\/python-programs.com\/python-programming-list-methods\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/python-programs.com\/python-programming-list-methods\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/python-programs.com\/python-programming-list-methods\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/python-programs.com\/"},{"@type":"ListItem","position":2,"name":"Python Programming \u2013 List Methods"}]},{"@type":"Article","@id":"https:\/\/python-programs.com\/python-programming-list-methods\/#article","isPartOf":{"@id":"https:\/\/python-programs.com\/python-programming-list-methods\/#webpage"},"author":{"@id":"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb"},"headline":"Python Programming \u2013 List Methods","datePublished":"2021-04-22T12:08:01+00:00","dateModified":"2021-11-22T13:09:21+00:00","mainEntityOfPage":{"@id":"https:\/\/python-programs.com\/python-programming-list-methods\/#webpage"},"wordCount":457,"commentCount":0,"publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/python-programs.com\/python-programming-list-methods\/#respond"]}]},{"@type":"Person","@id":"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb","name":"Prasanna","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/174540ad43736c7d1a4c4f83c775e74d?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/174540ad43736c7d1a4c4f83c775e74d?s=96&d=mm&r=g","caption":"Prasanna"},"url":"https:\/\/python-programs.com\/author\/prasanna\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/3408"}],"collection":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/comments?post=3408"}],"version-history":[{"count":2,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/3408\/revisions"}],"predecessor-version":[{"id":3410,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/3408\/revisions\/3410"}],"wp:attachment":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/media?parent=3408"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/categories?post=3408"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/tags?post=3408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}