{"id":3470,"date":"2021-04-23T09:55:22","date_gmt":"2021-04-23T04:25:22","guid":{"rendered":"https:\/\/python-programs.com\/?p=3470"},"modified":"2021-11-22T18:39:21","modified_gmt":"2021-11-22T13:09:21","slug":"python-programming-data-structures-list-using-square-brackets","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-programming-data-structures-list-using-square-brackets\/","title":{"rendered":"Python Programming \u2013 Data Structures \u2013 List Using Square Brackets"},"content":{"rendered":"

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

Python Programming \u2013 Data Structures \u2013 List Using Square Brackets<\/h2>\n

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

The list can be created in many ways.<\/p>\n

Using square brackets<\/strong><\/p>\n

As discussed before, the most common way of creating a list is by enclosing comma-separated values (items) between square brackets. Simply assigning a square bracket to a variable creates an empty list.<\/p>\n

>>> a= [ ]\r\n>>> a\r\n[ ]\r\n>>> type ( a ) \r\n< type ' list \u2019 ><\/pre>\n

Using other lists<\/strong><\/p>\n

A list can also be created by copying a list or slicing a list.<\/p>\n

>>> a= [ ' spam ' , ' eggs ' , 100 , 1234 ]\r\n>>> b=a [ : ]\r\n>>> b\r\n[ ' spam ' , ' eggs ' , 100 , 1234 ]\r\n>>> c=a [ 1 : 3 ]\r\n>>> c\r\n[ ' eggs ' , 100 ]<\/pre>\n

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

List comprehension provides a concise way to create a list. A list comprehension consists of brackets containing an expression followed by a for clause, then zero or more for or if clauses. The result will be a new list resulting from evaluating the expression in the context of the for and if clauses that follow it.<\/p>\n

Common applications are to make new lists where each element is the result of some operations applied to each member of another sequence or iterable or to create a sub-sequence of those elements that satisfy a certain condition. For example, creating a list of squares using an elaborate approach is as follows:<\/p>\n

>>> squares= [ ]\r\n>>> for x in range ( 10 ) :\r\n. . . squares. append ( x**2 )\r\n. . .\r\n>>> squares\r\n[ 0 , 1 , 4 , 9 , 16 , 25 , 36 , 49 , 64 , 81 ]<\/pre>\n

The same can be obtained using list comprehension as:<\/p>\n

squares= [ x**2 for x in range ( 10 ) ] \r\n>>> squares\r\n[ 0 , 1 , 4 , 9 , 16 , 25 , 36 , 49 , 64 , 81 ]<\/pre>\n

Alternatively, it can also be obtained using map ( ) built-in function:<\/p>\n

squares=map ( 1ambda x : x**2 , range ( 10 ) )\r\n>>> squares\r\n[ 0 , 1 , 4 , 9 , 16 , 25 , 36 , 49 , 64 , 81 ]<\/pre>\n

The following list comprehension combines the elements of two lists if they are not equal:<\/p>\n

>>> [ ( x , y ) for x in [ 1 , 2 , 3 ] for y in [ 3 , 1 , 4 ] if x !=y ]\r\n[ ( 1 , 3 ) , ( 1 , 4 ) , ( 2 , 3 ) , ( 2 , 1 ) , ( 2 , 4 ) , ( 3 , 1 ) , ( 3 , 4 ) ]<\/pre>\n

and it’s equivalent to:<\/p>\n

>>> combs= [ ]\r\n>>> for x in [ 1 , 2 , 3 ] :\r\n. . . for y in [ 3 , 1 , 4 ] :\r\n. . . if x !=y :\r\n. . . combs. append ( ( x , y ) )\r\n. . . \r\n>>> combs\r\n[ ( 1 , 3 ) , ( 1 , 4 ) , ( 2 , 3 ) , ( 2 , 1 ) , ( 2 , 4 ) , ( 3 , 1 ) , ( 3 , 4 ) ]<\/pre>\n

Using built-in function<\/p>\n

The list can also be created using a built-in function list ( ). The list ( [iterable] ) function returns a list whose items are the same and in the same order as iterable items. The iterable can be either a sequence, a container that supports iteration, or an iterator object. If iterable is already a list, a copy is made and returned. If no argument is given, a new empty list is returned.<\/p>\n

>>> list ( ( ' hi ' ' hello ' , ' bye ' ) )\r\n[ ' hi ' , ' hello ' , ' bye ' ]\r\n>>> list ( ' hello ' )\r\n[ ' h ' , ' e ' , ' 1 ' , ' 1 ' , ' o ' ]\r\n>>> list ( ( 10 , 50 , ) )\r\n[ 10 , 50 ]\r\n>>> list ( )\r\n[ ]<\/pre>\n

Accessing list elements<\/strong><\/p>\n

Like string indices, list indices start at 0. To access values in a list, use the square brackets with the index or indices to obtain a slice of the list.<\/p>\n

>>> a= [ ' spam ' , ' eggs ' , 100 , 1234 ]\r\n>>> a [ 0 ] \r\n' spam ' \r\n>>> a [ 0 ] [ 1 ]\r\n' p '\r\n>>> a [ 1 : 3 ] \r\n[ ' eggs ' , 100 ]<\/pre>\n","protected":false},"excerpt":{"rendered":"

In this Page, We are Providing Python Programming \u2013 Data Structures \u2013 List Using Square Brackets. Students can visit for more Detail and Explanation of Python Handwritten Notes\u00a0Pdf. Python Programming \u2013 Data Structures \u2013 List Using Square Brackets List creation The list can be created in many ways. Using square brackets As discussed before, the …<\/p>\n

Python Programming \u2013 Data Structures \u2013 List Using Square Brackets<\/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 Data Structures \u2013 List Using Square Brackets - 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-data-structures-list-using-square-brackets\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Programming \u2013 Data Structures \u2013 List Using Square Brackets - Python Programs\" \/>\n<meta property=\"og:description\" content=\"In this Page, We are Providing Python Programming \u2013 Data Structures \u2013 List Using Square Brackets. Students can visit for more Detail and Explanation of Python Handwritten Notes\u00a0Pdf. Python Programming \u2013 Data Structures \u2013 List Using Square Brackets List creation The list can be created in many ways. Using square brackets As discussed before, the … Python Programming \u2013 Data Structures \u2013 List Using Square Brackets Read More »\" \/>\n<meta property=\"og:url\" content=\"https:\/\/python-programs.com\/python-programming-data-structures-list-using-square-brackets\/\" \/>\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-23T04:25:22+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=\"3 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-data-structures-list-using-square-brackets\/#webpage\",\"url\":\"https:\/\/python-programs.com\/python-programming-data-structures-list-using-square-brackets\/\",\"name\":\"Python Programming \u2013 Data Structures \u2013 List Using Square Brackets - Python Programs\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/#website\"},\"datePublished\":\"2021-04-23T04:25:22+00:00\",\"dateModified\":\"2021-11-22T13:09:21+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/python-programs.com\/python-programming-data-structures-list-using-square-brackets\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/python-programs.com\/python-programming-data-structures-list-using-square-brackets\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/python-programs.com\/python-programming-data-structures-list-using-square-brackets\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/python-programs.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Programming \u2013 Data Structures \u2013 List Using Square Brackets\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/python-programs.com\/python-programming-data-structures-list-using-square-brackets\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/python-programming-data-structures-list-using-square-brackets\/#webpage\"},\"author\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb\"},\"headline\":\"Python Programming \u2013 Data Structures \u2013 List Using Square Brackets\",\"datePublished\":\"2021-04-23T04:25:22+00:00\",\"dateModified\":\"2021-11-22T13:09:21+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/python-programs.com\/python-programming-data-structures-list-using-square-brackets\/#webpage\"},\"wordCount\":358,\"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-data-structures-list-using-square-brackets\/#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 Data Structures \u2013 List Using Square Brackets - 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-data-structures-list-using-square-brackets\/","og_locale":"en_US","og_type":"article","og_title":"Python Programming \u2013 Data Structures \u2013 List Using Square Brackets - Python Programs","og_description":"In this Page, We are Providing Python Programming \u2013 Data Structures \u2013 List Using Square Brackets. Students can visit for more Detail and Explanation of Python Handwritten Notes\u00a0Pdf. Python Programming \u2013 Data Structures \u2013 List Using Square Brackets List creation The list can be created in many ways. Using square brackets As discussed before, the … Python Programming \u2013 Data Structures \u2013 List Using Square Brackets Read More »","og_url":"https:\/\/python-programs.com\/python-programming-data-structures-list-using-square-brackets\/","og_site_name":"Python Programs","article_publisher":"https:\/\/www.facebook.com\/btechgeeks","article_published_time":"2021-04-23T04:25:22+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":"3 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-data-structures-list-using-square-brackets\/#webpage","url":"https:\/\/python-programs.com\/python-programming-data-structures-list-using-square-brackets\/","name":"Python Programming \u2013 Data Structures \u2013 List Using Square Brackets - Python Programs","isPartOf":{"@id":"https:\/\/python-programs.com\/#website"},"datePublished":"2021-04-23T04:25:22+00:00","dateModified":"2021-11-22T13:09:21+00:00","breadcrumb":{"@id":"https:\/\/python-programs.com\/python-programming-data-structures-list-using-square-brackets\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/python-programs.com\/python-programming-data-structures-list-using-square-brackets\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/python-programs.com\/python-programming-data-structures-list-using-square-brackets\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/python-programs.com\/"},{"@type":"ListItem","position":2,"name":"Python Programming \u2013 Data Structures \u2013 List Using Square Brackets"}]},{"@type":"Article","@id":"https:\/\/python-programs.com\/python-programming-data-structures-list-using-square-brackets\/#article","isPartOf":{"@id":"https:\/\/python-programs.com\/python-programming-data-structures-list-using-square-brackets\/#webpage"},"author":{"@id":"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb"},"headline":"Python Programming \u2013 Data Structures \u2013 List Using Square Brackets","datePublished":"2021-04-23T04:25:22+00:00","dateModified":"2021-11-22T13:09:21+00:00","mainEntityOfPage":{"@id":"https:\/\/python-programs.com\/python-programming-data-structures-list-using-square-brackets\/#webpage"},"wordCount":358,"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-data-structures-list-using-square-brackets\/#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\/3470"}],"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=3470"}],"version-history":[{"count":1,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/3470\/revisions"}],"predecessor-version":[{"id":3471,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/3470\/revisions\/3471"}],"wp:attachment":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/media?parent=3470"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/categories?post=3470"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/tags?post=3470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}