{"id":4110,"date":"2022-01-26T17:46:15","date_gmt":"2022-01-26T17:46:15","guid":{"rendered":"https:\/\/ibex.tech\/python\/?p=4110"},"modified":"2025-05-15T17:54:07","modified_gmt":"2025-05-15T16:54:07","slug":"list-general","status":"publish","type":"post","link":"https:\/\/ibex.tech\/python\/memory-python\/lists-python\/list-general","title":{"rendered":".List general"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Create a list<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>#Create an empty list\nmy_list = &#91;]\n\nmy_list = &#91;0, 1, 2, 3, 4, 5]\n\n#An list of one byte (with a value of 17):\nmy_list = &#91;17]\n\n#A list of 5 elements each with a value of None\nmy_list = &#91;None] * 5\n\n#A list of 5 elements each with a value of 0\nmy_list = &#91;0] * 5\n\n#Alternative way to create a list 5 entries long containing 0\nmy_list = &#91;0 for i in range(5)]<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Multiple data types<\/h5>\n\n\n\n<p>A list can contain different data types<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_list = &#91;\"Dave\", 13, 46, True, 0.3]<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Adding to a list<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>my_list.append(0x12)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Accessing list elements<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>for index in xrange(0, 24):\n    led_values&#91;index] = 0]<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Multi dimension lists<\/h4>\n\n\n\n<p>See <a href=\"\/python\/memory-python\/lists-python\/multidimensional-lists\">here<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Read list items<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>print(my_list&#91;2])\n\n#You can also access in reverse in python by using negative indexes\nprint(my_list&#91;-1])    #Will print the last item in the list\nprint(my_list&#91;-2])    #Will print the second to last item in the list<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Get a slice of list items<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>MyNewArray = my_list&#91;2:6]    #New array has items from index 2 to 5 (2nd parameter is 1 more than that wanted)\n\n\nmy_list_new = my_list&#91;:3]     #New array has first 3 items from index\nmy_list_new = my_list&#91;-2:]    #New array has last 2 items from index<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Manipulate lists<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">Add list items<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>my_list.append('Hi')      #Add to end\n\nmy_list.insert(0, 'Hi')   #Insert at index 0<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Joins 2 lists \/ concatenate<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>my_list = &#91;10, 13, 46, 28]\nmy_list_new = my_list + &#91;13, 22]<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Remove list item<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>#Remove at index\nmy_list.pop(2)        #Remove the list item at index 2\nItem = my_list.pop()  #Remove the list item at the last index and assign its value to the variable Item\n\n\n#Remove item with value\nmy_list.remove('Hi')    #Remove the first list item that matches the string specified (will error if not present)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Length<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>Length = len(my_list)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Sort List<\/h4>\n\n\n\n<p>You can sort values and also strings alphabetically<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#Sort an array\nmy_list.sort()    #Just this (no need for my_list= )\nmy_list.sort(reverse=True)    #Sort in reverse order\n\n#Copy an array and then sort it\nmy_list_new = sorted(my_list)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Is value in list<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>if ('Hi' in my_list):<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">How many times does value occur in a list<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>num_of_times = my_list.count('Hi')<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Create a list Multiple data types A list can contain different data types Adding to a list Accessing list elements Multi dimension lists See here Read list items Get a slice of list items Manipulate lists Add list items Joins 2 lists \/ concatenate Remove list item Length Sort List You can sort values and [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[325],"tags":[],"class_list":["post-4110","post","type-post","status-publish","format-standard","hentry","category-lists-python"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4110","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/comments?post=4110"}],"version-history":[{"count":26,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4110\/revisions"}],"predecessor-version":[{"id":4936,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4110\/revisions\/4936"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/media?parent=4110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/categories?post=4110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/tags?post=4110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}