Skip to content

Instantly share code, notes, and snippets.

@mGalarnyk
Last active May 29, 2017 21:07
Show Gist options
  • Select an option

  • Save mGalarnyk/31bf4bbf39a50dbba78896ecdac8e050 to your computer and use it in GitHub Desktop.

Select an option

Save mGalarnyk/31bf4bbf39a50dbba78896ecdac8e050 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Lists"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Lists are written within square brackets [ ]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" | | | |\n",
"--- | --- | --- | --- | ---\n",
"z =| [3, | 7, | 4, | 2]\n",
"index | 0 | 1 | 2 | 3"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Defining a list\n",
"z = [3, 7, 4, 2]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Accessing Values in List"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# The first element of a list is at index 0\n",
"z[0]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"z[2]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Access Last Element of List \n",
"z[-2]"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"### Slicing Lists"
]
},
{
"cell_type": "code",
"execution_count": 120,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[3, 7]"
]
},
"execution_count": 120,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# first index is inclusive (before the :) and last (after the :) is not. \n",
"# not including index 2\n",
"z[0:2]"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[3, 7, 4]"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# everything up to index 3\n",
"z[:3]"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[7, 4, 2]"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# index 1 to end of list\n",
"z[1:]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Minimum, Maximum, Length, and Sum of a list"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2 7 4 16\n"
]
}
],
"source": [
"print(min(z), max(z), len(z), sum(z))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Count Number of Times Value is in List"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"random_list = [4, 1, 5, 4, 10, 4]\n",
"random_list.count(4)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Return First Index of Value"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" | | | | | |\n",
"--- | --- | --- | --- | --- | --- | ---\n",
"random_list =| [4, | 1, | 5, | 4, | 10, | 4]\n",
"index=| 0 | 1 | 2 | 3 | 4 | 5"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"random_list.index(4)"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 75,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# you can specify where you start your search\n",
"random_list.index(4, 3)"
]
},
{
"cell_type": "code",
"execution_count": 180,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 180,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# random_list.index(value, [start, stop])\n",
"random_list.index(4, 5, 6)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Sorting a list "
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"x = [3, 7, 2, 11, 8, 10, 4]\n",
"y = ['Steve', 'Rachel', 'Michael', 'Adam', 'Monica', 'Jessica', 'Lester']"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2, 3, 4, 7, 8, 10, 11]\n"
]
}
],
"source": [
"# Sorting and Altering original list\n",
"# low to high\n",
"x.sort()\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[11, 10, 8, 7, 4, 3, 2]\n"
]
}
],
"source": [
"# Sorting and Altering original list\n",
"# high to low\n",
"x.sort(reverse = True)\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Adam', 'Jessica', 'Lester', 'Michael', 'Monica', 'Rachel', 'Steve']\n"
]
}
],
"source": [
"# Sorting and Altering original list\n",
"# A-Z\n",
"y.sort()\n",
"print(y)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Steve', 'Rachel', 'Monica', 'Michael', 'Lester', 'Jessica', 'Adam']\n"
]
}
],
"source": [
"# Sorting and Altering original list\n",
"# Z-A\n",
"y.sort(reverse = True)\n",
"print(y)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Adam', 'Jessica', 'Lester', 'Michael', 'Monica', 'Rachel', 'Steve']"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# sorting list WITHOUT altering original list \n",
"new_list = sorted(y)\n",
"new_list"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Steve', 'Rachel', 'Monica', 'Michael', 'Lester', 'Jessica', 'Adam']"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# notice y is unchanged\n",
"y"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Add to the End of List"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[11, 10, 8, 7, 4, 3, 2]"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[11, 10, 8, 7, 4, 3, 2, 3]\n"
]
}
],
"source": [
"x.append(3)\n",
"print(x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Remove First Occurence of Value in List"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[11, 8, 7, 4, 3, 2, 3]\n"
]
}
],
"source": [
"x.remove(10)\n",
"print(x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Remove Item at the Index you Provide"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[11, 8, 7, 4, 3, 2, 3]\n"
]
}
],
"source": [
"# List before you remove an item\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Remove item at the index\n",
"# this function will also return the item you removed from the list\n",
"# Default is the last index\n",
"x.pop(3)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[11, 8, 7, 3, 2, 3]\n"
]
}
],
"source": [
"print(x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Extend (Useful for Concatenating Lists)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Extend list by appending elements (iterable)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"x.extend([4, 5])"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[11, 8, 7, 3, 2, 3, 4, 5]"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can also use + for concatenating lists"
]
},
{
"cell_type": "code",
"execution_count": 195,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x+y= [11, 8, 7, 3, 2, 3, 4, 5, 'Steve', 'Rachel', 'Monica', 'Michael', 'Lester', 'Jessica', 'Adam']\n"
]
}
],
"source": [
"# lists can be diverse, each element in the list can be of a different type.\n",
"# lists are really list of pointers, and these pointers can \n",
"# point to anything.\n",
"\n",
"# Concatenating Lists\n",
"print('x+y=',x+y)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Insert Item before the Index you Provide"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[11, 8, 7, 3, 2, 3, 4, 5]"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "code",
"execution_count": 197,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"x.insert(4, [4, 5])"
]
},
{
"cell_type": "code",
"execution_count": 198,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[11, 8, 7, 3, [4, 5], 2, 3, 4, 5]"
]
},
"execution_count": 198,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**if this tutorial doesn't cover what you are looking for, please leave a comment on the youtube video and I will try to cover what you are interested in. (Please subscribe if you can!)**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"https://www.youtube.com/watch?v=w9I8R3WSVqc"
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [conda env:py36]",
"language": "python",
"name": "conda-env-py36-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.1"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment