{
"cells": [
{
"cell_type": "markdown",
"id": "rising-andorra",
"metadata": {},
"source": [
"# Lists, ranges, tuples, for, list-comprehensions, enumerate, zip (17/2-2021)"
]
},
{
"cell_type": "markdown",
"id": "sorted-angola",
"metadata": {},
"source": [
"## Exercise\n",
"\n",
"Given a list of even length, create a list where each consecutive pair of elements has been swapped, e.g. [1, 2, 3, 4, 5, 6] becomes [2, 1, 4, 3, 6, 5]."
]
},
{
"cell_type": "markdown",
"id": "buried-warrant",
"metadata": {},
"source": [
"Below are 14 different solutions"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "orange-avenue",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3, 4, 5, 6] -> [2, 1, 4, 3, 6, 5]\n"
]
}
],
"source": [
"# Invariant:\n",
"# X = [x, y, x, y, x, y]\n",
"# |\n",
"# i\n",
"# Y = [y, x, y, x]\n",
"\n",
"\n",
"# Solution with while-loop and append one element at a time\n",
"\n",
"X = [1, 2, 3, 4, 5, 6]\n",
"\n",
"Y = []\n",
"\n",
"i = 0\n",
"while i < len(X):\n",
" # print('i =', i, ' Y =', Y)\n",
" if i % 2 == 1:\n",
" Y.append(X[i - 1])\n",
" else:\n",
" Y.append(X[i + 1])\n",
" i += 1\n",
" \n",
"print(X, '->', Y)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "persistent-console",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3, 4, 5, 6] -> [2, 1, 4, 3, 6, 5]\n"
]
}
],
"source": [
"# x if b else y\n",
"\n",
"X = [1, 2, 3, 4, 5, 6]\n",
"\n",
"Y = []\n",
"\n",
"i = 0\n",
"while i < len(X):\n",
" # print('i =', i, ' Y =', Y)\n",
" Y.append(X[i - 1] if i % 2 == 1 else X[i + 1])\n",
" i += 1\n",
" \n",
"print(X, '->', Y)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "champion-bronze",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3, 4, 5, 6] -> [2, 1, 4, 3, 6, 5]\n"
]
}
],
"source": [
"# Solution with while-loop and two appends per iteration\n",
"\n",
"X = [1, 2, 3, 4, 5, 6]\n",
"\n",
"Y = []\n",
"\n",
"i = 0\n",
"while i < len(X):\n",
" # print('i =', i, ' Y =', Y)\n",
" Y.append(X[i + 1])\n",
" Y.append(X[i])\n",
" i += 2\n",
" \n",
"print(X, '->', Y)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "sweet-underwear",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3, 4, 5, 6] -> [2, 1, 4, 3, 6, 5]\n"
]
}
],
"source": [
"# use a range and for-loop instead of a while\n",
"\n",
"X = [1, 2, 3, 4, 5, 6]\n",
"Y = []\n",
"for i in range(0, len(X), 2):\n",
" # print(Y)\n",
" Y.append(X[i + 1])\n",
" Y.append(X[i])\n",
"print(X, '->', Y)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "exact-present",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3, 4, 5, 6] -> [2, 1, 4, 3, 6, 5]\n"
]
}
],
"source": [
"# use extend instead of 2 x append\n",
"\n",
"X = [1, 2, 3, 4, 5, 6]\n",
"Y = []\n",
"for i in range(0, len(X), 2):\n",
" # print(Y)\n",
" Y.extend([X[i + 1], X[i]])\n",
"print(X, '->', Y)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "british-shakespeare",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3, 4, 5, 6] -> [2, 1, 4, 3, 6, 5]\n"
]
}
],
"source": [
"# use list slicing\n",
"\n",
"X = [1, 2, 3, 4, 5, 6]\n",
"\n",
"#print(X[1:4])\n",
"#print(X[3:0:-1])\n",
"\n",
"Y = []\n",
"for i in range(0, len(X), 2):\n",
" #print('i =', i, 'Y =', Y)\n",
" ## does not work, because 0 - 1 = -1, would be last element in the list\n",
" # Y.extend(X[i + 1:i - 1:-1]) \n",
" Y.extend(X[i:i + 2][::-1])\n",
"print(X, '->', Y)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "level-fleece",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3, 4, 5, 6] -> [2, 1, 4, 3, 6, 5]\n"
]
}
],
"source": [
"# could also have used reversed\n",
"\n",
"X = [1, 2, 3, 4, 5, 6]\n",
"\n",
"Y = []\n",
"for i in range(0, len(X), 2):\n",
" Y.extend(reversed(X[i:i + 2]))\n",
"print(X, '->', Y)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "architectural-lunch",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"X = [1, 2, 3]\n",
"Y = \n",
"list(Y) = [3, 2, 1]\n",
"list(Y) = []\n",
"[1, 2, 3]\n"
]
}
],
"source": [
"# reversed() returns something like a range(), that can be run over - but only once\n",
"\n",
"X = [1,2,3]\n",
"Y = reversed(X) # returns an iterable object\n",
"print('X =', X)\n",
"print('Y =', Y)\n",
"print('list(Y) =', list(Y))\n",
"print('list(Y) =', list(Y)) # OOPS... Y is exhausted by now\n",
"#print(reversed(reversed(X))) # cannot reverse an reversed object\n",
"print(list(reversed(list(reversed(X))))) # cannot reverse an reversed object"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "later-furniture",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2, 1, 4, 3, 6, 5]\n"
]
}
],
"source": [
"X = [1, 2, 3, 4, 5, 6]\n",
"\n",
"# print(X[0::2])\n",
"# print(X[1::2])\n",
"Y = [None] * len(X)\n",
"# print(Y)\n",
"Y[1::2] = X[0::2]\n",
"# print(Y)\n",
"Y[0::2] = X[1::2]\n",
"print(Y)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "banner-rainbow",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"7 42\n",
"7 7\n",
"7 7\n"
]
}
],
"source": [
"# swapping to elements the wrong way\n",
"\n",
"a = 7\n",
"b = 42\n",
"print(a, b)\n",
"b = a\n",
"print(a, b)\n",
"a = b\n",
"print(a, b)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "assured-peoples",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"7 42\n",
"7 7 42\n",
"a=7 b=7 tmp=42\n",
"42 7\n"
]
}
],
"source": [
"a = 7\n",
"b = 42\n",
"print(a, b)\n",
"tmp = b\n",
"b = a\n",
"print(a, b, tmp)\n",
"print(f'{a=} {b=} {tmp=}')\n",
"# print(f'{(a, b, tmp)=}')\n",
"a = tmp\n",
"print(a, b)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "dietary-spyware",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"7 42\n",
"42 7\n"
]
}
],
"source": [
"a = 7\n",
"b = 42\n",
"print(a, b)\n",
"a, b = b, a\n",
"print(a, b)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "previous-butter",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2, 1, 4, 3, 6, 5]\n"
]
}
],
"source": [
"# swapping entries in a list\n",
"\n",
"X = [1, 2, 3, 4, 5, 6]\n",
"\n",
"for i in range(0, len(X), 2):\n",
" # print(i, X)\n",
" X[i], X[i + 1] = X[i + 1], X[i]\n",
"print(X)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "substantial-spanking",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2, 1, 4, 3, 6, 5]\n"
]
}
],
"source": [
"# swapping entries in a list\n",
"\n",
"X = [1, 2, 3, 4, 5, 6]\n",
"\n",
"for i in range(0, len(X), 2):\n",
" # print(i, X)\n",
" X[i], X[i + 1] = X[i + 1], X[i]\n",
" \n",
"print(X)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "temporal-sellers",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2, 1, 4, 3, 6, 5]\n"
]
}
],
"source": [
"# slice assignment\n",
"\n",
"X = [1, 2, 3, 4, 5, 6]\n",
"\n",
"for i in range(0, len(X), 2):\n",
" # print(i, X)\n",
" X[i:i + 2] = X[i:i+2][::-1]\n",
" \n",
"print(X)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "leading-parallel",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2, 1, 4, 3, 6, 5]\n"
]
}
],
"source": [
"# swapping slices\n",
"\n",
"X = [1, 2, 3, 4, 5, 6]\n",
"X[::2], X[1::2] = X[1::2], X[::2] \n",
"print(X)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "continuing-potter",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3, 4, 5, 6] -> [2, 1, 4, 3, 6, 5]\n"
]
}
],
"source": [
"# list comprehension\n",
"\n",
"X = [1, 2, 3, 4, 5, 6]\n",
"\n",
"Y = [(X[i - 1] if i % 2 == 1 else X[i + 1]) for i in range(len(X))]\n",
"\n",
"print(X, '->', Y)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "discrete-worship",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3, 4, 5, 6] -> [2, 1, 4, 3, 6, 5]\n"
]
}
],
"source": [
"# list comprehension with two for-loops\n",
"\n",
"X = [1, 2, 3, 4, 5, 6]\n",
"\n",
"# Y = [X[i:i+2][::-1] for i in range(0, len(X), 2)] # wrong list of lists\n",
"Y = [v for i in range(0, len(X), 2) for v in X[i:i + 2][::-1]]\n",
"\n",
"print(X, '->', Y)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "constant-mobile",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2, 1, 4, 3, 6, 5]\n"
]
}
],
"source": [
"X = [1, 2, 3, 4, 5, 6]\n",
"\n",
"print([e for pair in zip(X[1::2], X[0::2]) for e in pair])"
]
},
{
"cell_type": "markdown",
"id": "demonstrated-reconstruction",
"metadata": {},
"source": [
"## Exercise\n",
"\n",
"Convert a list of strings to a single nice string, e.g. ['A', 'B', 'C', 'D'] becomes 'A, B, C and D'."
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "terminal-ready",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"A, B, C and D\n"
]
}
],
"source": [
"S = ['A', 'B', 'C', 'D']\n",
"T = S[0]\n",
"for s in S[1:-1]:\n",
" T = T + ', ' + s\n",
"T += ' and ' + S[-1]\n",
"print(T)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "original-gazette",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"A, B, C and D\n"
]
}
],
"source": [
"S = ['A', 'B', 'C', 'D']\n",
"T = ', '.join(S[:-1]) + ' and ' + S[-1] if len(S) > 1 else S[-1]\n",
"print(T)"
]
},
{
"cell_type": "markdown",
"id": "swiss-minute",
"metadata": {},
"source": [
"## Exercise\n",
"\n",
"Remove all odd numbers from a list, e.g. [2, 3, 7, 4, 3, 2, 6, 1, 8] becomes [2, 4, 2, 6, \n",
"8]."
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "qualified-steal",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2, 3, 7, 4, 3, 2, 6, 1, 8]\n",
"[2, 3, 4, 3, 2, 6, 1, 8]\n",
"[2, 3, 4, 3, 6, 1, 8]\n"
]
}
],
"source": [
"X = [2, 3, 7, 4, 3, 2, 6, 1, 8]\n",
"print(X)\n",
"X.remove(7) # remove element\n",
"#X.remove(9) # error, since 9 not in X\n",
"print(X)\n",
"del X[4] # remove at position 4\n",
"print(X)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "biblical-appearance",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2 [2, 3, 7, 4, 3, 2, 6, 1, 8]\n",
"3 [2, 3, 7, 4, 3, 2, 6, 1, 8]\n",
"4 [2, 7, 4, 3, 2, 6, 1, 8]\n",
"3 [2, 7, 4, 3, 2, 6, 1, 8]\n",
"6 [2, 7, 4, 2, 6, 1, 8]\n",
"1 [2, 7, 4, 2, 6, 1, 8]\n",
"[2, 7, 4, 2, 6, 8]\n"
]
}
],
"source": [
"# wrong solution\n",
"\n",
"X = [2, 3, 7, 4, 3, 2, 6, 1, 8]\n",
"for x in X: # fails since X gets shifted by .remove()\n",
" print(x, X)\n",
" if x % 2 == 1:\n",
" X.remove(x)\n",
"print(X)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "suitable-defendant",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2 [2, 3, 7, 4, 3, 2, 6, 1, 8] [2, 3, 7, 4, 3, 2, 6, 1, 8]\n",
"3 [2, 3, 7, 4, 3, 2, 6, 1, 8] [2, 3, 7, 4, 3, 2, 6, 1, 8]\n",
"7 [2, 3, 7, 4, 3, 2, 6, 1, 8] [2, 7, 4, 3, 2, 6, 1, 8]\n",
"4 [2, 3, 7, 4, 3, 2, 6, 1, 8] [2, 4, 3, 2, 6, 1, 8]\n",
"3 [2, 3, 7, 4, 3, 2, 6, 1, 8] [2, 4, 3, 2, 6, 1, 8]\n",
"2 [2, 3, 7, 4, 3, 2, 6, 1, 8] [2, 4, 2, 6, 1, 8]\n",
"6 [2, 3, 7, 4, 3, 2, 6, 1, 8] [2, 4, 2, 6, 1, 8]\n",
"1 [2, 3, 7, 4, 3, 2, 6, 1, 8] [2, 4, 2, 6, 1, 8]\n",
"8 [2, 3, 7, 4, 3, 2, 6, 1, 8] [2, 4, 2, 6, 8]\n",
"[2, 4, 2, 6, 8]\n"
]
}
],
"source": [
"# correct solution\n",
"\n",
"X = [2, 3, 7, 4, 3, 2, 6, 1, 8]\n",
"Y = X.copy()\n",
"for x in Y:\n",
"# for x in X.copy():\n",
"# for x in X[:]:\n",
"# for x in list(X):\n",
" print(x, Y, X)\n",
" if x % 2 == 1:\n",
" X.remove(x)\n",
"print(X)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "correct-cosmetic",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 2 [2, 3, 7, 4, 3, 2, 6, 1, 8]\n",
"1 3 [2, 3, 7, 4, 3, 2, 6, 1, 8]\n",
"2 4 [2, 7, 4, 3, 2, 6, 1, 8]\n",
"3 3 [2, 7, 4, 3, 2, 6, 1, 8]\n",
"4 6 [2, 7, 4, 2, 6, 1, 8]\n",
"5 1 [2, 7, 4, 2, 6, 1, 8]\n"
]
},
{
"ename": "IndexError",
"evalue": "list index out of range",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0mX\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m7\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m4\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m6\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m8\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mX\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mX\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mX\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 6\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mX\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m%\u001b[0m \u001b[1;36m2\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[1;32mdel\u001b[0m \u001b[0mX\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;31m# shifts X\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mIndexError\u001b[0m: list index out of range"
]
}
],
"source": [
"# wrong solution\n",
"\n",
"X = [2, 3, 7, 4, 3, 2, 6, 1, 8]\n",
"for i in range(len(X)):\n",
" print(i, X[i], X)\n",
" if X[i] % 2 == 1:\n",
" del X[i] # shifts X\n",
"print(X)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "perceived-benchmark",
"metadata": {},
"outputs": [],
"source": [
"# correct solution\n",
"\n",
"X = [2, 3, 7, 4, 3, 2, 6, 1, 8]\n",
" print(i, X[i], X)\n",
" if X[i] % 2 == 1:\n",
" del X[i] # shifts X\n",
"print(X)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "final-intermediate",
"metadata": {},
"outputs": [],
"source": [
"# efficient solution, never shifts a list\n",
"\n",
"X = [2, 3, 7, 4, 3, 2, 6, 1, 8]\n",
"Y = []\n",
"for x in X:\n",
" if x % 2 == 0:\n",
" Y.append(x)\n",
"print(X, '->', Y)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "sudden-intervention",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2, 3, 7, 4, 3, 2, 6, 1, 8] -> [2, 4, 2, 6, 8]\n"
]
}
],
"source": [
"# list comprehension\n",
"\n",
"X = [2, 3, 7, 4, 3, 2, 6, 1, 8]\n",
"Y = [x for x in X if x % 2 == 0]\n",
"print(X, '->', Y)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "abandoned-throw",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2, 4, 2, 6, 8]\n"
]
}
],
"source": [
"# while, don't advance i if odd\n",
"\n",
"i = 0\n",
"while i < len(X):\n",
"# print(i, X)\n",
" if X[i] % 2 == 1:\n",
" del X[i]\n",
" else:\n",
" i = i + 1\n",
"print(X)"
]
},
{
"cell_type": "markdown",
"id": "copyrighted-suggestion",
"metadata": {},
"source": [
"## Exercise\n",
"\n",
"Given three lists of integers X, Y, Z, generate list of all tuples (x, y, z) where x is in X, y in Y, z in Z, and z = x + y."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "associate-mercy",
"metadata": {},
"outputs": [],
"source": [
"# .append solution\n",
"\n",
"X = [1,2,4,5,8]\n",
"Y = [1,3,6,9]\n",
"Z = [6,8,14,16]\n",
"\n",
"answer = []\n",
"for x in X:\n",
" for y in Y:\n",
" for z in Z:\n",
" if z == x + y:\n",
" answer.append((x, y, z))\n",
"print(answer)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "musical-public",
"metadata": {},
"outputs": [],
"source": [
"# list comprehension\n",
"\n",
"answer = [(x, y, z) for x in X for y in Y for z in Z if z == x + y]\n",
"print(answer)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "imposed-miller",
"metadata": {},
"outputs": [],
"source": [
"# use \"in Z\"\n",
"\n",
"answer = [(x, y, x + y) for x in X for y in Y if x + y in Z]\n",
"print(answer)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "returning-spouse",
"metadata": {},
"outputs": [],
"source": [
"# avoid computing x + y twice using :=\n",
"\n",
"answer = [(x, y, z) for x in X for y in Y if (z := x + y) in Z]\n",
"print(answer)"
]
},
{
"cell_type": "markdown",
"id": "assumed-birth",
"metadata": {},
"source": [
"## Exercise\n",
"\n",
"Given two lists X and Y, find all pairs (i, j) where X[i] = Y[j]."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dependent-transport",
"metadata": {},
"outputs": [],
"source": [
"# range\n",
"\n",
"X = [2, 5, 3, 2, 4]\n",
"Y = [3, 7, 2, 4, 2]\n",
"\n",
"for i in range(len(X)):\n",
" for j in range(len(Y)):\n",
" if X[i] == Y[j]:\n",
" print((i, j))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "expired-earth",
"metadata": {},
"outputs": [],
"source": [
"# enumerate\n",
"\n",
"X = [2, 5, 3, 2, 4]\n",
"Y = [3, 7, 2, 4, 2]\n",
"\n",
"for i, x in enumerate(X):\n",
" for j, y in enumerate(Y):\n",
" if x == y:\n",
" print((i, j))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "medieval-buying",
"metadata": {},
"outputs": [],
"source": [
"# list comprehension\n",
"\n",
"print([(i, j) for i, x in enumerate(X) for j, y in enumerate(Y) if x == y])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "horizontal-dancing",
"metadata": {},
"outputs": [],
"source": [
"# make lines shorter\n",
" \n",
"print([(i, j) \n",
" for i, x in enumerate(X) \n",
" for j, y in enumerate(Y) \n",
" if x == y])"
]
}
],
"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.9.1"
}
},
"nbformat": 4,
"nbformat_minor": 5
}