{"id":132,"date":"2016-11-27T21:32:27","date_gmt":"2016-11-27T21:32:27","guid":{"rendered":"http:\/\/python.wp.w3.pt\/?p=132"},"modified":"2016-11-27T22:42:10","modified_gmt":"2016-11-27T22:42:10","slug":"listas","status":"publish","type":"post","link":"http:\/\/python.w3.pt\/?p=132","title":{"rendered":"Listas"},"content":{"rendered":"<p>Considere-se a lista seguinte, assim como algumas opera\u00e7\u00f5es de consulta da lista.<\/p>\n<pre>&gt;&gt;&gt; myList = [\"The\", \"earth\", \"revolves\", \"around\", \"sun\"]\r\n&gt;&gt;&gt; myList\r\n['The', 'earth', 'revolves', 'around', 'sun']\r\n\r\n&gt;&gt;&gt; myList[0]\r\n'The'\r\n\r\n&gt;&gt;&gt; myList[4]\r\n'sun'\r\n\r\n&gt;&gt;&gt; myList[5]\r\nTraceback (most recent call last):\r\n  File \"\", line 1, in \r\nIndexError: list index out of range\r\n<\/pre>\n<p>Um \u00edndice negativo consulta a lista a partir da extremidade final<\/p>\n<pre>&gt;&gt;&gt; myList[-1]\r\n'sun'\r\n<\/pre>\n<p>Podem inserir-se elementos em posi\u00e7\u00f5es determinadas.<\/p>\n<pre>&gt;&gt;&gt; myList.insert(0,\"Yes\")\r\n&gt;&gt;&gt; myList\r\n['Yes', 'The', 'earth', 'revolves', 'around', 'sun']\r\n<\/pre>\n<p>Para adicionar elementos \u00e0 lista, \u00e9 poss\u00edvel usar a fun\u00e7\u00e3o <em>append<\/em> ou a fun\u00e7\u00e3o <em>extend<\/em>. A primeira adiciona apenas um elemento. A segunda, se o elemento for uma lista, estende-a com os elementos dessa lista.<\/p>\n<pre>&gt;&gt;&gt; myList.append([\"a\", \"true\"])\r\n\r\n&gt;&gt;&gt; myList\r\n['Yes', 'The', 'earth', 'revolves', 'around', 'sun', ['a', 'true']]\r\n\r\n&gt;&gt;&gt; len(myList)\r\n7\r\n<\/pre>\n<pre>&gt;&gt;&gt; myList.extend([\"statement\", \"for\", \"sure\"])\r\n\r\n&gt;&gt;&gt; myList\r\n['Yes', 'The', 'earth', 'revolves', 'around', 'sun', ['a', 'true'], 'statement', 'for', 'sure']\r\n\r\n&gt;&gt;&gt; len(myList)\r\n10\r\n<\/pre>\n<p>Podem extrair-se elementos de uma lista com a fun\u00e7\u00e3o <em>slice<\/em>.<\/p>\n<pre>\r\n>>> myList[1:4]\r\n['The', 'earth', 'revolves']\r\n\r\n>>> myList[:4]\r\n['Yes', 'The', 'earth', 'revolves']\r\n\r\n>>> myList[4:]\r\n['around', 'sun', ['a', 'true'], 'statement', 'for', 'sure']\r\n\r\n>>> myList[:]\r\n['Yes', 'The', 'earth', 'revolves', 'around', 'sun', ['a', 'true'], 'statement', 'for', 'sure']\r\n<\/pre>\n<p>Pesquisa em listas a partir do conte\u00fado:<\/p>\n<pre>\r\n>>> myList.index(\"revolves\")\r\n3\r\n\r\n>>> myList.index(\"a\")\r\nTraceback (most recent call last):\r\n  File \"<stdin>\", line 1, in <module>\r\nValueError: 'a' is not in list\r\n\r\n>>> myList.index([\"a\", \"true\"])\r\n6\r\n\r\n>>> \"sun\" in myList\r\nTrue\r\n<\/pre>\n<p>Remover elementos:<\/p>\n<pre>\r\n>>> myList\r\n['Yes', 'The', 'earth', 'revolves', 'around', 'sun', ['a', 'true'], 'statement', 'for', 'sure']\r\n\r\n>>> myList.remove(\"Yes\")\r\n>>> myList\r\n['The', 'earth', 'revolves', 'around', 'sun', ['a', 'true'], 'statement', 'for', 'sure']\r\n\r\n>>> myList.remove(\"statement\")\r\n>>> myList\r\n['The', 'earth', 'revolves', 'around', 'sun', ['a', 'true'], 'for', 'sure']\r\n\r\n>>> myList.remove([\"a\", \"true\"])\r\n>>> myList\r\n['The', 'earth', 'revolves', 'around', 'sun', 'for', 'sure']\r\n\r\n>>> myList.pop()\r\n'sure'\r\n>>> myList\r\n['The', 'earth', 'revolves', 'around', 'sun', 'for']\r\n<\/pre>\n<p>Operadores de listas<\/p>\n<pre>\r\n>>> myList\r\n['The', 'earth', 'revolves', 'around', 'sun', 'for']\r\n>>> myList = myList + [\"sure\"]\r\n>>> myList\r\n['The', 'earth', 'revolves', 'around', 'sun', 'for', 'sure']\r\n\r\n>>> myList += [\".\"]\r\n>>> myList\r\n['The', 'earth', 'revolves', 'around', 'sun', 'for', 'sure', '.']\r\n\r\n>>> myList *= 2\r\n>>> myList\r\n['The', 'earth', 'revolves', 'around', 'sun', 'for', 'sure', '.', 'The', 'earth', 'revolves', 'around', 'sun', 'for', 'sure', '.']\r\n<\/pre>\n<pre>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Considere-se a lista seguinte, assim como algumas opera\u00e7\u00f5es de consulta da lista. &gt;&gt;&gt; myList = [&#8220;The&#8221;, &#8220;earth&#8221;, &#8220;revolves&#8221;, &#8220;around&#8221;, &#8220;sun&#8221;] &gt;&gt;&gt; myList [&#8216;The&#8217;, &#8216;earth&#8217;, &#8216;revolves&#8217;, &#8216;around&#8217;, &#8216;sun&#8217;] &gt;&gt;&gt; myList[0] &#8216;The&#8217; &gt;&gt;&gt; myList[4] &#8216;sun&#8217; &gt;&gt;&gt; myList[5] Traceback (most recent call last): File &#8220;&#8221;, line 1, in IndexError: list index out of range Um \u00edndice negativo consulta &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/python.w3.pt\/?p=132\" class=\"more-link\">Continuar a ler <span class=\"screen-reader-text\">&#8220;Listas&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/python.w3.pt\/index.php?rest_route=\/wp\/v2\/posts\/132"}],"collection":[{"href":"http:\/\/python.w3.pt\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/python.w3.pt\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/python.w3.pt\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/python.w3.pt\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=132"}],"version-history":[{"count":4,"href":"http:\/\/python.w3.pt\/index.php?rest_route=\/wp\/v2\/posts\/132\/revisions"}],"predecessor-version":[{"id":135,"href":"http:\/\/python.w3.pt\/index.php?rest_route=\/wp\/v2\/posts\/132\/revisions\/135"}],"wp:attachment":[{"href":"http:\/\/python.w3.pt\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/python.w3.pt\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=132"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/python.w3.pt\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}