diff --git a/deep_copy.ipynb b/deep_copy.ipynb new file mode 100644 index 00000000..a11d7052 --- /dev/null +++ b/deep_copy.ipynb @@ -0,0 +1,248 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Python: how to Copy and Deep Copy Python Lists \n", + "(c) Joe James 2023" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Assignment is not a Copy\n", + "listA = listB does not create a copy. Changes to one list will be reflected in the other.\n", + "listA and listB both reference the exact same list." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2, 44, 6, [1, 3]]\n", + "140554034568968\n", + "140554034568968\n" + ] + } + ], + "source": [ + "listA = [2, 4, 6, [1, 3]]\n", + "listB = listA\n", + "listB[1] = 44\n", + "print(listA)\n", + "print(id(listA))\n", + "print(id(listB))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Shallow copy using the list() constructor\n", + "Shallow copy only works for 1D lists of native data types. \n", + "Sublists, dicts, and other objects will retain the same referece to those objects." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2, 4, 6, [55, 3]]\n" + ] + } + ], + "source": [ + "listA = [2, 4, 6, [1, 3]]\n", + "listB = list(listA)\n", + "listB[1] = 44\n", + "listB[3][0] = 55\n", + "print(listA)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Other ways to make a Shallow copy\n", + "List comprehensions, list.copy(), or copy.copy() can also be used to make *shallow* copies" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2, 4, 6, [55, 3]]\n" + ] + } + ], + "source": [ + "listA = [2, 4, 6, [1, 3]]\n", + "listB = [x for x in listA]\n", + "listB[1] = 44\n", + "listB[3][0] = 55\n", + "print(listA)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2, 4, 6, [55, 3]]\n" + ] + } + ], + "source": [ + "listA = [2, 4, 6, [1, 3]]\n", + "listB = listA.copy()\n", + "listB[1] = 44\n", + "listB[3][0] = 55\n", + "print(listA)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2, 4, 6, [55, 3]]\n" + ] + } + ], + "source": [ + "import copy\n", + "listA = [2, 4, 6, [1, 3]]\n", + "listB = copy.copy(listA)\n", + "listB[1] = 44\n", + "listB[3][0] = 55\n", + "print(listA)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### How to Deep Copy a Python List\n", + "use copy.deepcopy()" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2, 4, 6, [1, 3]]\n" + ] + } + ], + "source": [ + "import copy\n", + "listA = [2, 4, 6, [1, 3]]\n", + "listB = copy.deepcopy(listA)\n", + "listB[1] = 44\n", + "listB[3][0] = 55\n", + "print(listA)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Deepcopy with Objects" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "140554035637216 140554035637104\n", + "140554035637216 140554035637216\n", + "140554035637216 140554035637048\n" + ] + } + ], + "source": [ + "class Pony():\n", + " def __init__(self, n):\n", + " self.name = n\n", + " \n", + "# copy.copy on an object gives you 2 unique objects (with same attributes)\n", + "pony1 = Pony('Pinto')\n", + "pony2 = copy.copy(pony1)\n", + "print(id(pony1), id(pony2))\n", + "\n", + "# copy.copy on a list of objects gives you 2 unique lists containing the exact same objects \n", + "# (ie. new list is a shallow copy)\n", + "m = [pony1, pony2]\n", + "n = copy.copy (m)\n", + "print(id(m[0]), id(n[0]))\n", + "\n", + "# use copy.deepcopy to deep copy a list of objects\n", + "n = copy.deepcopy (m)\n", + "print(id(m[0]), id(n[0]))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "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.7.0" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/dict_comprehensions.py b/dict_comprehensions.py new file mode 100644 index 00000000..890c7221 --- /dev/null +++ b/dict_comprehensions.py @@ -0,0 +1,54 @@ +# Python Dictionary Comprehensions +# (c) Joe James 2023 + +# 1. math function to compute values using list +dict1 = {x: 2*x for x in [0, 2, 4, 6]} +print ('1. ', dict1) + +# 2. math function to compute values using range +dict2 = {x: x**2 for x in range(0, 7, 2)} +print ('2. ', dict2) + +# 3. from chars in a string +dict3 = {x: ord(x) for x in 'Kumar'} +print ('3. ', dict3) + +# 4. given lists of keys & values +x = ['Aditii', 'Brandon', 'Clumley', 'Magomed', 'Rishi'] +y = [1, 2, 3, 13, 18] +dict4 = {i: j for (i,j) in zip(x,y)} +print ('4. ', dict4) + +# 5. from chars in a string +x = "python" +dict5 = {i: 3*i.upper() for i in x} +print('5. ', dict5) + +# 6. list comprehension for the value +x = [2, 4, 6, 8] +y = [5, 10, 15, 20] +dict6 = {i: [i + 2*j for j in y] for i in x} +print('6. ', dict6) + +#7. using items +x = {'A':10, 'B':20, 'C':30} +dict7 = {i: j*2 for (i,j) in x.items()} +print('7. ', dict7) + +# 8. conditional comprehension +dict8 = {i: i**3 for i in range(10) if i%2 == 0} +print('8. ', dict8) + +# 9. if-else conditional comprehension +x = {'A':10, 'B':20, 'C':30} +dict9 = {i: (j if j < 15 else j+100) for (i,j) in x.items()} +print('9. ', dict9) + +# 10. transformation from an existing dict +x = {'A':10, 'B':20, 'C':30} +dict10 = {i: x[i]+1 for i in x} +print('10. ', dict10) + + + + diff --git a/flatten_list.py b/flatten_list.py new file mode 100644 index 00000000..3f3c57df --- /dev/null +++ b/flatten_list.py @@ -0,0 +1,27 @@ +# Python Flatten Nested Lists +# (c) Joe James 2023 + +# list comprehension method +def flatten1 (myList): + return [i for j in myList for i in j] + +# recursive method +def flatten2 (myList): + flatList = [] + for item in myList: + if isinstance(item, list): + flatList.extend(flatten2(item)) + else: + flatList.append(item) + return flatList + +list1 = [[0], [1, 2], [3, [4, 5]], [6], [7]] +list2 = [0, [1, 2], [3, [4, 5]], [6], 7] + +print("flatten1(list1): ", flatten1(list1)) # works, but only flattens 1 layer of sublists +# print(flatten1(list2)) # error - can't work with list of ints and sublists of ints + +print("flatten2(list1): ", flatten2(list1)) +print("flatten2(list2): ", flatten2(list2)) + + diff --git a/remove_from_list.py b/remove_from_list.py new file mode 100644 index 00000000..9619664f --- /dev/null +++ b/remove_from_list.py @@ -0,0 +1,48 @@ +# Python: del vs pop vs remove from a list +# (c) Joe James 2023 + +def get_dogs(): + return ['Fido', 'Rover', 'Spot', 'Duke', 'Chip', 'Spot'] + +dogs = get_dogs() +print(dogs) + +# Use pop() to remove last item or an item by index and get the returned value. +print('1. pop last item from list:') +myDog = dogs.pop() +print(myDog, dogs) + +dogs = get_dogs() +print('2. pop item with index 1:') +myDog = dogs.pop(1) +print(myDog, dogs) + +# Use remove() to delete an item by value. (raises ValueError if value not found) +dogs = get_dogs() +print('3. remove first Spot from list:') +dogs.remove('Spot') +print(dogs) + +# Use del to remove an item or range of items by index. Or delete entire list. +dogs = get_dogs() +print('4. del item with index 3:') +del(dogs[3]) +print(dogs) + +dogs = get_dogs() +print('5. del items [1:3] from list:') +del(dogs[1:3]) +print(dogs) + +dogs = get_dogs() +print('6. del entire list:') +del(dogs) +print(dogs) + + + + + + + +