Skip to content

Instantly share code, notes, and snippets.

@gustavothomas17
Created August 23, 2021 17:38
Show Gist options
  • Save gustavothomas17/bccbd3d547629b72a439baf8329bedcd to your computer and use it in GitHub Desktop.
Save gustavothomas17/bccbd3d547629b72a439baf8329bedcd to your computer and use it in GitHub Desktop.
Lists Python
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Lists in Python\n",
"\n",
"Estimated time needed: **15** minutes\n",
"\n",
"## Objectives\n",
"\n",
"* Perform list operations in Python, including indexing, list manipulation, and copy/clone list.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2>Table of Contents</h2>\n",
"<div class=\"alert alert-block alert-info\" style=\"margin-thttps://op/?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_https://co/?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm_id=NA-SkillsNetwork-Channel-SkillsNetworkCoursesIBMDeveloperSkillsNetworkPY0101ENSkillsNetwork19487395-2021-01-01ntent=000026UJ&utm_term=10006555&utm_id=NA-SkillsNetwork-Channel-SkillsNetworkCoursesIBMDeveloperSkillsNetworkPY0101ENSkillsNetwork19487395-2021-01-01: 20px\">\n",
" <ul>\n",
" <li>\n",
" <a href=\"https://#dataset\">About the Dataset</a>\n",
" </li>\n",
" <li>\n",
" <a href=\"https://#list\">Lists</a>\n",
" <ul>\n",
" <li><a href=\"https://index/?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_https://content/?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm_id=NA-SkillsNetwork-Channel-SkillsNetworkCoursesIBMDeveloperSkillsNetworkPY0101ENSkillsNetwork19487395-2021-01-01=000026UJ&utm_term=10006555&utm_id=NA-SkillsNetwork-Channel-SkillsNetworkCoursesIBMDeveloperSkillsNetworkPY0101ENSkillsNetwork19487395-2021-01-01\">Indexing</a></li>\n",
" <li><a href=\"content\">List Content</a></li>\n",
" <li><a href=\"op\">List Operations</a></li>\n",
" <li><a href=\"co\">Copy and Clone List</a></li>\n",
" </ul>\n",
" </li>\n",
" <li>\n",
" <a href=\"https://#quiz\">Quiz on Lists</a>\n",
" </li>\n",
" </ul>\n",
"\n",
"</div>\n",
"\n",
"<hr>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"#dataset\">About the Dataset</h2>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Imagine you received album recommendations from your friends and compiled all of the recommandations into a table, with specific information about each album.\n",
"\n",
"The table has one row for each movie and several columns:\n",
"\n",
"* **artist** - Name of the artist\n",
"* **album** - Name of the album\n",
"* **released_year** - Year the album was released\n",
"* **length_min_sec** - Length of the album (hours,minutes,seconds)\n",
"* **genre** - Genre of the album\n",
"* **music_recording_sales_millions** - Music recording sales (millions in USD) on [SONG://DATABASE](http://www.song-database.com/?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm_id=NA-SkillsNetwork-Channel-SkillsNetworkCoursesIBMDeveloperSkillsNetworkPY0101ENSkillsNetwork19487395-2021-01-01)\n",
"* **claimed_sales_millions** - Album's claimed sales (millions in USD) on [SONG://DATABASE](http://www.song-database.com/?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm_id=NA-SkillsNetwork-Channel-SkillsNetworkCoursesIBMDeveloperSkillsNetworkPY0101ENSkillsNetwork19487395-2021-01-01)\n",
"* **date_released** - Date on which the album was released\n",
"* **soundtrack** - Indicates if the album is the movie soundtrack (Y) or (N)\n",
"* **rating_of_friends** - Indicates the rating from your friends from 1 to 10\n",
"\n",
"<br>\n",
"<br>\n",
"\n",
"The dataset can be seen below:\n",
"\n",
"<font size=\"1\">\n",
"<table font-size:xx-small>\n",
" <tr>\n",
" <th>Artist</th>\n",
" <th>Album</th> \n",
" <th>Released</th>\n",
" <th>Length</th>\n",
" <th>Genre</th> \n",
" <th>Music recording sales (millions)</th>\n",
" <th>Claimed sales (millions)</th>\n",
" <th>Released</th>\n",
" <th>Soundtrack</th>\n",
" <th>Rating (friends)</th>\n",
" </tr>\n",
" <tr>\n",
" <td>Michael Jackson</td>\n",
" <td>Thriller</td> \n",
" <td>1982</td>\n",
" <td>00:42:19</td>\n",
" <td>Pop, rock, R&B</td>\n",
" <td>46</td>\n",
" <td>65</td>\n",
" <td>30-Nov-82</td>\n",
" <td></td>\n",
" <td>10.0</td>\n",
" </tr>\n",
" <tr>\n",
" <td>AC/DC</td>\n",
" <td>Back in Black</td> \n",
" <td>1980</td>\n",
" <td>00:42:11</td>\n",
" <td>Hard rock</td>\n",
" <td>26.1</td>\n",
" <td>50</td>\n",
" <td>25-Jul-80</td>\n",
" <td></td>\n",
" <td>8.5</td>\n",
" </tr>\n",
" <tr>\n",
" <td>Pink Floyd</td>\n",
" <td>The Dark Side of the Moon</td> \n",
" <td>1973</td>\n",
" <td>00:42:49</td>\n",
" <td>Progressive rock</td>\n",
" <td>24.2</td>\n",
" <td>45</td>\n",
" <td>01-Mar-73</td>\n",
" <td></td>\n",
" <td>9.5</td>\n",
" </tr>\n",
" <tr>\n",
" <td>Whitney Houston</td>\n",
" <td>The Bodyguard</td> \n",
" <td>1992</td>\n",
" <td>00:57:44</td>\n",
" <td>Soundtrack/R&B, soul, pop</td>\n",
" <td>26.1</td>\n",
" <td>50</td>\n",
" <td>25-Jul-80</td>\n",
" <td>Y</td>\n",
" <td>7.0</td>\n",
" </tr>\n",
" <tr>\n",
" <td>Meat Loaf</td>\n",
" <td>Bat Out of Hell</td> \n",
" <td>1977</td>\n",
" <td>00:46:33</td>\n",
" <td>Hard rock, progressive rock</td>\n",
" <td>20.6</td>\n",
" <td>43</td>\n",
" <td>21-Oct-77</td>\n",
" <td></td>\n",
" <td>7.0</td>\n",
" </tr>\n",
" <tr>\n",
" <td>Eagles</td>\n",
" <td>Their Greatest Hits (1971-1975)</td> \n",
" <td>1976</td>\n",
" <td>00:43:08</td>\n",
" <td>Rock, soft rock, folk rock</td>\n",
" <td>32.2</td>\n",
" <td>42</td>\n",
" <td>17-Feb-76</td>\n",
" <td></td>\n",
" <td>9.5</td>\n",
" </tr>\n",
" <tr>\n",
" <td>Bee Gees</td>\n",
" <td>Saturday Night Fever</td> \n",
" <td>1977</td>\n",
" <td>1:15:54</td>\n",
" <td>Disco</td>\n",
" <td>20.6</td>\n",
" <td>40</td>\n",
" <td>15-Nov-77</td>\n",
" <td>Y</td>\n",
" <td>9.0</td>\n",
" </tr>\n",
" <tr>\n",
" <td>Fleetwood Mac</td>\n",
" <td>Rumours</td> \n",
" <td>1977</td>\n",
" <td>00:40:01</td>\n",
" <td>Soft rock</td>\n",
" <td>27.9</td>\n",
" <td>40</td>\n",
" <td>04-Feb-77</td>\n",
" <td></td>\n",
" <td>9.5</td>\n",
" </tr>\n",
"</table></font>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"list\">Lists</h2>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3 id=\"index\">Indexing</h3>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We are going to take a look at lists in Python. A list is a sequenced collection of different objects such as integers, strings, and even other lists as well. The address of each element within a list is called an <b>index</b>. An index is used to access and refer to items within a list.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/labs/Module%202/images/ListsIndex.png\" width=\"1000\" />\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To create a list, type the list within square brackets <b>\\[ ]</b>, with your content inside the parenthesis and separated by commas. Let’s try it!\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Michael Jackson', 10.1, 1982]"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Create a list\n",
"\n",
"L = [\"Michael Jackson\", 10.1, 1982]\n",
"L"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can use negative and regular indexing with a list:\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/labs/Module%202/images/ListsNeg.png\" width=\"1000\" />\n"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"the same element using negative and positive indexing:\n",
" Postive: Michael Jackson \n",
" Negative: Michael Jackson\n",
"the same element using negative and positive indexing:\n",
" Postive: 10.1 \n",
" Negative: 10.1\n",
"the same element using negative and positive indexing:\n",
" Postive: 1982 \n",
" Negative: 1982\n"
]
}
],
"source": [
"# Print the elements on each index\n",
"\n",
"print('the same element using negative and positive indexing:\\n Postive:',L[0],\n",
"'\\n Negative:' , L[-3] )\n",
"print('the same element using negative and positive indexing:\\n Postive:',L[1],\n",
"'\\n Negative:' , L[-2] )\n",
"print('the same element using negative and positive indexing:\\n Postive:',L[2],\n",
"'\\n Negative:' , L[-1] )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"<h3 id=\"content\">List Content</h3>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Lists can contain strings, floats, and integers. We can nest other lists, and we can also nest tuples and other data structures. The same indexing conventions apply for nesting:\n"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'A'"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Sample List\n",
"a = [\"Michael Jackson\", 10.1, 1982, [1, 2], (\"A\", 1)]\n",
"a[4][0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3 id=\"op\">List Operations</h3>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also perform slicing in lists. For example, if we want the last two elements, we use the following command:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Sample List\n",
"\n",
"L = [\"Michael Jackson\", 10.1,1982,\"MJ\",1]\n",
"L"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/labs/Module%202/images/ListsSlice.png\" width=\"1000\">\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# List slicing\n",
"\n",
"L[3:5]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can use the method <code>extend</code> to add new elements to the list:\n"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on NoneType object:\n",
"\n",
"class NoneType(object)\n",
" | Methods defined here:\n",
" | \n",
" | __bool__(self, /)\n",
" | self != 0\n",
" | \n",
" | __new__(*args, **kwargs) from builtins.type\n",
" | Create and return a new object. See help(type) for accurate signature.\n",
" | \n",
" | __repr__(self, /)\n",
" | Return repr(self).\n",
"\n"
]
}
],
"source": [
"# Use extend to add elements to list\n",
"\n",
"L = [ \"Michael Jackson\", 10.2]\n",
"help(L.extend(['pop', 10]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Another similar method is <code>append</code>. If we apply <code>append</code> instead of <code>extend</code>, we add one element to the list:\n"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Michael Jackson', 10.2, ['pop', 10]]"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Use append to add elements to list\n",
"\n",
"L = [ \"Michael Jackson\", 10.2]\n",
"L.append(['pop', 10])\n",
"L"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Each time we apply a method, the list changes. If we apply <code>extend</code> we add two new elements to the list. The list <code>L</code> is then modified by adding two new elements:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Use extend to add elements to list\n",
"\n",
"L = [ \"Michael Jackson\", 10.2]\n",
"L.extend(['pop', 10])\n",
"L"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If we append the list <code>\\['a','b']</code> we have one new element consisting of a nested list:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Use append to add elements to list\n",
"\n",
"L.append(['a','b'])\n",
"L"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As lists are mutable, we can change them. For example, we can change the first element as follows:\n"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Before change: ['disco', 10, 1.2]\n",
"After change: ['hard rock', 10, 1.2]\n"
]
}
],
"source": [
"# Change the element based on the index\n",
"\n",
"A = [\"disco\", 10, 1.2]\n",
"print('Before change:', A)\n",
"A[0] = 'hard rock'\n",
"print('After change:', A)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also delete an element of a list using the <code>del</code> command:\n"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Before change: ['hard rock', 10, 1.2]\n",
"After change: [10, 1.2]\n"
]
}
],
"source": [
"# Delete the element based on the index\n",
"\n",
"print('Before change:', A)\n",
"del(A[0])\n",
"print('After change:', A)"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[10, 1, 1]"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A.extend([1])\n",
"del(A[1])\n",
"A"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can convert a string to a list using <code>split</code>. For example, the method <code>split</code> translates every group of characters separated by a space into an element in a list:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Split the string, default is by space\n",
"\n",
"'hard rock'.split()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can use the split function to separate strings on a specific character which we call a **delimiter**. We pass the character we would like to split on into the argument, which in this case is a comma. The result is a list, and each element corresponds to a set of characters that have been separated by a comma:\n"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['A', 'B,C,D']"
]
},
"execution_count": 65,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Split the string by comma\n",
"\n",
"'A ,B,C,D'.split(' ,')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3 id=\"co\">Copy and Clone List</h3>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When we set one variable <b>B</b> equal to <b>A</b>, both <b>A</b> and <b>B</b> are referencing the same list in memory:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Copy (copy by reference) the list A\n",
"\n",
"A = [\"hard rock\", 10, 1.2]\n",
"B = A\n",
"print('A:', A)\n",
"print('B:', B)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/labs/Module%202/images/ListsRef.png\" width=\"1000\" align=\"center\">\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Initially, the value of the first element in <b>B</b> is set as \"hard rock\". If we change the first element in <b>A</b> to <b>\"banana\"</b>, we get an unexpected side effect. As <b>A</b> and <b>B</b> are referencing the same list, if we change list <b>A</b>, then list <b>B</b> also changes. If we check the first element of <b>B</b> we get \"banana\" instead of \"hard rock\":\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Examine the copy by reference\n",
"\n",
"print('B[0]:', B[0])\n",
"A[0] = \"banana\"\n",
"print('B[0]:', B[0])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This is demonstrated in the following figure:\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src = \"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/labs/Module%202/images/ListsRefGif.gif\" width=\"1000\" />\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can clone list **A** by using the following syntax:\n"
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[10, 1, 1]"
]
},
"execution_count": 69,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Clone (clone by value) the list A\n",
"\n",
"B = A[:]\n",
"A[1]=22\n",
"B"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Variable **B** references a new copy or clone of the original list. This is demonstrated in the following figure:\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/labs/Module%202/images/ListsVal.gif\" width=\"1000\" />\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now if you change <b>A</b>, <b>B</b> will not change:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print('B[0]:', B[0])\n",
"A[0] = \"hard rock\"\n",
"print('B[0]:', B[0])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"quiz\">Quiz on List</h2>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create a list <code>a_list</code>, with the following elements <code>1</code>, <code>hello</code>, <code>\\[1,2,3]</code> and <code>True</code>.\n"
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {},
"outputs": [],
"source": [
"# Write your code below and press Shift+Enter to execute\n",
"a_list = [1, 'hello', [1,2,3], True]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<details><summary>Click here for the solution</summary>\n",
"\n",
"```python\n",
"a_list = [1, 'hello', [1, 2, 3] , True]\n",
"a_list\n",
"\n",
"```\n",
"\n",
"</details>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Find the value stored at index 1 of <code>a_list</code>.\n"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {},
"outputs": [
{
"ename": "AttributeError",
"evalue": "'list' object has no attribute 'sorted'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-75-aa1a5460435f>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Write your code below and press Shift+Enter to execute\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0ma_list\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msorted\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m: 'list' object has no attribute 'sorted'"
]
}
],
"source": [
"# Write your code below and press Shift+Enter to execute\n",
"a_list.sorted()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<details><summary>Click here for the solution</summary>\n",
"\n",
"```python\n",
"a_list[1]\n",
"\n",
"```\n",
"\n",
"</details>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Retrieve the elements stored at index 1, 2 and 3 of <code>a_list</code>.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Write your code below and press Shift+Enter to execute\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<details><summary>Click here for the solution</summary>\n",
"\n",
"```python\n",
"a_list[1:4]\n",
"\n",
"```\n",
"\n",
"</details>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Concatenate the following lists <code>A = \\[1, 'a']</code> and <code>B = \\[2, 1, 'd']</code>:\n"
]
},
{
"cell_type": "code",
"execution_count": 76,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"[1, 'a', 2, 1, 'd']"
]
},
"execution_count": 76,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Write your code below and press Shift+Enter to execute\n",
"A = [1, 'a']\n",
"B =[2, 1, 'd']\n",
"A+B"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<details><summary>Click here for the solution</summary>\n",
"\n",
"```python\n",
"A = [1, 'a'] \n",
"B = [2, 1, 'd']\n",
"A + B\n",
"\n",
"```\n",
"\n",
"</details>\n"
],
"metadata": {
"kernelspec": {
"display_name": "Python",
"language": "python",
"name": "conda-env-python-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.13"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment