Skip to content

Instantly share code, notes, and snippets.

@salamituns
Created January 29, 2021 10:52
Show Gist options
  • Save salamituns/4bd8c733a599bf10fe8fd415e0846aeb to your computer and use it in GitHub Desktop.
Save salamituns/4bd8c733a599bf10fe8fd415e0846aeb to your computer and use it in GitHub Desktop.
Created on Skills Network Labs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A list is a sequenced collection of different objects such as integers, strings, and other lists as well.\n",
"\n",
"An index is used to access and refer to items within a list."
]
},
{
"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": [
"#A list is created within square brackets [ ]\n",
"\n",
"L = [\"Michael Jackson\", 10.1, 1982]\n",
"L"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Indexing the above list L, we can index them as 0, 1, 2...respective.\n",
" We can also index the list in negavite as -3, -2, -1.....respectively."
]
},
{
"cell_type": "code",
"execution_count": 2,
"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"
]
}
],
"source": [
"print('the same element using negative and positive indexing:\\n Postive:',L[0],\n",
"'\\n Negative:' , L[-3] )"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"the same element using negative and positive indexing:\n",
" Positve: 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 same element using negative and positive indexing:\\n Positve:', 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": [
"Lists can contain strings, floats, and integers. It can also nest Tuples and other data structures."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Michael Jackson', 10.1, 1982, [1, 2], ('A', 1)]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"L = [\"Michael Jackson\", 10.1, 1982, [1, 2], (\"A\", 1)]\n",
"L"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[1, 2], ('A', 1)]"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#List slicing...... e.g., to access the last two elements.\n",
"L[3:5]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In List, we use .extend to add new elements to a list."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Michael Jackson', 10.1, 1982, [1, 2], ('A', 1), 'pop', 10]"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"L.extend (['pop', 10])\n",
"L"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Michael Jackson', 10.1, 1982, [1, 2], ('A', 1), 'pop', 10, ['a', 'b']]"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#With .append, we can add a nested list to a list.\n",
"\n",
"L.append(['a','b'])\n",
"L"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"lists are mutable, we can change them"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Before change: ['disco', 10, 1.2]\n",
"After change: ['hard rock', 10, 1.2]\n"
]
}
],
"source": [
"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 'del' command:"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Before change: ['hard rock', 10, 1.2]\n",
"After change: [10, 1.2]\n"
]
}
],
"source": [
"print('Before change:', A)\n",
"del(A[0])\n",
"print('After change:', A)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can convert a string to a list using .split function in python"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['hard', 'rock']"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#e.g., we can split the string 'hard rock' to hard, rock using the .split function\n",
"'hard rock'.split()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can use the split function to separate strings on a specific character, and turn them to make a list."
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['A', 'B', 'C', 'D']"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'A,B,C,D'.split(',')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can copy and clone in List such that:\n",
"When we set one variable B equal to A; both A and B are referencing the same list in memory:\n",
"e.g., see the example below."
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"A: ['hard rock', 10, 1.2]\n",
"B: ['hard rock', 10, 1.2]\n"
]
}
],
"source": [
"A = [\"hard rock\", 10, 1.2]\n",
"B = A\n",
"print('A:', A)\n",
"print('B:', B)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"B[0]: hard rock\n",
"B[0]: banana\n"
]
}
],
"source": [
"#As A and B are referencing the same list, if we change list A, then list B also changes\n",
"\n",
"print('B[0]:', B[0])\n",
"A[0] = \"banana\"\n",
"print('B[0]:', B[0])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In the example below, We can clone list A by using the following syntax:"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['banana', 10, 1.2]"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#such that Variable B references a new copy or clone of the original list in A;\n",
"\n",
"B = A[:]\n",
"B"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"B[0]: banana\n",
"B[0]: banana\n"
]
}
],
"source": [
"#since variable B is now a clone copy of A, any changes in A will not affect B\n",
"\n",
"print('B[0]:', B[0])\n",
"A[0] = 'hard rock'\n",
"print('B[0]:', B[0])"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"A: ['hard rock', 10, 1.2]\n",
"B: ['banana', 10, 1.2]\n"
]
}
],
"source": [
"print ('A:', A)\n",
"print ('B:', B)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A task to create on list"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 'hello', [1, 2, 3], True]"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Create a random list composed of all popular data stuctures\n",
"\n",
"a_list = [1, 'hello', [1, 2, 3], True]\n",
"a_list"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['hello', [1, 2, 3], True]"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#retrieving the elements stored at index 1, 2, and 3.\n",
"a_list[1:4]"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"A = [1, 'A']\n",
"B = [2, 1, 'd']"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.12"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment