{ "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 }