317 lines
6.5 KiB
Plaintext
317 lines
6.5 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# [Lists](https://docs.python.org/3/library/stdtypes.html#lists)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"my_empty_list = []\n",
|
|
"print('empty list: {}, type: {}'.format(my_empty_list, type(my_empty_list)))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"list_of_ints = [1, 2, 6, 7]\n",
|
|
"list_of_misc = [0.2, 5, 'Python', 'is', 'still fun', '!']\n",
|
|
"print('lengths: {} and {}'.format(len(list_of_ints), len(list_of_misc)))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Accessing values"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"my_list = ['Python', 'is', 'still', 'cool']\n",
|
|
"print(my_list[0])\n",
|
|
"print(my_list[3])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"coordinates = [[12.0, 13.3], [0.6, 18.0], [88.0, 1.1]] # two dimensional\n",
|
|
"print('first coordinate: {}'.format(coordinates[0]))\n",
|
|
"print('second element of first coordinate: {}'.format(coordinates[0][1]))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Updating values"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"my_list = [0, 1, 2, 3, 4, 5]\n",
|
|
"my_list[0] = 99\n",
|
|
"print(my_list)\n",
|
|
"\n",
|
|
"# remove first value\n",
|
|
"del my_list[0]\n",
|
|
"print(my_list)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Checking if certain value is present in list"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"languages = ['Java', 'C++', 'Go', 'Python', 'JavaScript']\n",
|
|
"if 'Python' in languages:\n",
|
|
" print('Python is there!')"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"if 6 not in [1, 2, 3, 7]:\n",
|
|
" print('number 6 is not present')"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## List are mutable"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"original = [1, 2, 3]\n",
|
|
"modified = original\n",
|
|
"modified[0] = 99\n",
|
|
"print('original: {}, modified: {}'.format(original, modified))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"You can get around this by creating new `list`:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"original = [1, 2, 3]\n",
|
|
"modified = list(original) # Note list() \n",
|
|
"modified[0] = 99\n",
|
|
"print('original: {}, modified: {}'.format(original, modified))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## `list.append()`"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"my_list = [1]\n",
|
|
"my_list.append('ham')\n",
|
|
"print(my_list)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## `list.remove()`"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"my_list = ['Python', 'is', 'sometimes', 'fun']\n",
|
|
"my_list.remove('sometimes')\n",
|
|
"print(my_list)\n",
|
|
"\n",
|
|
"# If you are not sure that the value is in list, better to check first:\n",
|
|
"if 'Java' in my_list:\n",
|
|
" my_list.remove('Java')\n",
|
|
"else:\n",
|
|
" print('Java is not part of this story.')"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## `list.sort()`"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"numbers = [8, 1, 6, 5, 10]\n",
|
|
"numbers.sort()\n",
|
|
"print('numbers: {}'.format(numbers))\n",
|
|
"\n",
|
|
"numbers.sort(reverse=True)\n",
|
|
"print('numbers reversed: {}'.format(numbers))\n",
|
|
"\n",
|
|
"words = ['this', 'is', 'a', 'list', 'of', 'words']\n",
|
|
"words.sort()\n",
|
|
"print('words: {}'.format(words))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## `sorted(list)`\n",
|
|
"While `list.sort()` sorts the list in-place, `sorted(list)` returns a new list and leaves the original untouched:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"numbers = [8, 1, 6, 5, 10]\n",
|
|
"sorted_numbers = sorted(numbers)\n",
|
|
"print('numbers: {}, sorted: {}'.format(numbers, sorted_numbers))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## `list.extend()`"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"first_list = ['beef', 'ham']\n",
|
|
"second_list = ['potatoes',1 ,3]\n",
|
|
"first_list.extend(second_list)\n",
|
|
"print('first: {}, second: {}'.format(first_list, second_list))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Alternatively you can also extend lists by summing them:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"first = [1, 2, 3]\n",
|
|
"second = [4, 5]\n",
|
|
"first += second # same as: first = first + second\n",
|
|
"print('first: {}'.format(first))\n",
|
|
"\n",
|
|
"# If you need a new list\n",
|
|
"summed = first + second\n",
|
|
"print('summed: {}'.format(summed))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## `list.reverse()`"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"my_list = ['a', 'b', 'ham']\n",
|
|
"my_list.reverse()\n",
|
|
"print(my_list)"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"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.5.4"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|