|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "f189c3fd", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Python: Everything\n", |
| 9 | + "- 2) Lists: Copying\n", |
| 10 | + "<br>----------------------------------------------\n", |
| 11 | + "<br> By Hamed Shah-Hosseini\n", |
| 12 | + "<br> https://github.com/ostad-ai/Python-Everything\n", |
| 13 | + "<br> https://www.pinterest.com/HamedShahHosseini/programming-languages/python\n", |
| 14 | + "<br> Explanation in **Persian** language at Instagram: @words.persian" |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "markdown", |
| 19 | + "id": "78beeaf8", |
| 20 | + "metadata": {}, |
| 21 | + "source": [ |
| 22 | + "To copy a list with keyword $list$ or : , as shown below:" |
| 23 | + ] |
| 24 | + }, |
| 25 | + { |
| 26 | + "cell_type": "code", |
| 27 | + "execution_count": 17, |
| 28 | + "id": "e2fa4e97", |
| 29 | + "metadata": {}, |
| 30 | + "outputs": [ |
| 31 | + { |
| 32 | + "name": "stdout", |
| 33 | + "output_type": "stream", |
| 34 | + "text": [ |
| 35 | + "86545920 for [1, 2, 3, 'hello', 'world']\n", |
| 36 | + "82048192 for [1, 2, 3, 'hello', 'world']\n", |
| 37 | + "86545152 for [1, 2, 3, 'hello', 'world']\n" |
| 38 | + ] |
| 39 | + } |
| 40 | + ], |
| 41 | + "source": [ |
| 42 | + "mylist=[1,2,3,'hello','world']\n", |
| 43 | + "mylist2=list(mylist)\n", |
| 44 | + "mylist3=mylist[:]\n", |
| 45 | + "print(f'{id(mylist)} for {mylist}')\n", |
| 46 | + "print(f'{id(mylist2)} for {mylist2}')\n", |
| 47 | + "print(f'{id(mylist3)} for {mylist3}')" |
| 48 | + ] |
| 49 | + }, |
| 50 | + { |
| 51 | + "cell_type": "markdown", |
| 52 | + "id": "71985d4c", |
| 53 | + "metadata": {}, |
| 54 | + "source": [ |
| 55 | + "To copy a list with $copy$:" |
| 56 | + ] |
| 57 | + }, |
| 58 | + { |
| 59 | + "cell_type": "code", |
| 60 | + "execution_count": 18, |
| 61 | + "id": "0ddc46e1", |
| 62 | + "metadata": {}, |
| 63 | + "outputs": [ |
| 64 | + { |
| 65 | + "name": "stdout", |
| 66 | + "output_type": "stream", |
| 67 | + "text": [ |
| 68 | + "86541312 for [1, 2, 3, 'hello', 'world']\n", |
| 69 | + "82025600 for [1, 2, 3, 'hello', 'world']\n" |
| 70 | + ] |
| 71 | + } |
| 72 | + ], |
| 73 | + "source": [ |
| 74 | + "mylist=[1,2,3,'hello','world']\n", |
| 75 | + "mylist2=mylist.copy()\n", |
| 76 | + "print(f'{id(mylist)} for {mylist}')\n", |
| 77 | + "print(f'{id(mylist2)} for {mylist2}')" |
| 78 | + ] |
| 79 | + }, |
| 80 | + { |
| 81 | + "cell_type": "markdown", |
| 82 | + "id": "574d3a0b", |
| 83 | + "metadata": {}, |
| 84 | + "source": [ |
| 85 | + "To copy a nested list, we should use $deepcopy$:" |
| 86 | + ] |
| 87 | + }, |
| 88 | + { |
| 89 | + "cell_type": "code", |
| 90 | + "execution_count": 19, |
| 91 | + "id": "b02995f7", |
| 92 | + "metadata": {}, |
| 93 | + "outputs": [ |
| 94 | + { |
| 95 | + "name": "stdout", |
| 96 | + "output_type": "stream", |
| 97 | + "text": [ |
| 98 | + "82028352 for [[1, 2, 3], ['hello', 'world']]\n", |
| 99 | + "86534144 for [[1, 2, 3], ['hello', 'world']]\n" |
| 100 | + ] |
| 101 | + } |
| 102 | + ], |
| 103 | + "source": [ |
| 104 | + "from copy import deepcopy\n", |
| 105 | + "mylist=[[1,2,3],['hello','world']]\n", |
| 106 | + "mylist2=deepcopy(mylist)\n", |
| 107 | + "print(f'{id(mylist)} for {mylist}')\n", |
| 108 | + "print(f'{id(mylist2)} for {mylist2}')" |
| 109 | + ] |
| 110 | + }, |
| 111 | + { |
| 112 | + "cell_type": "code", |
| 113 | + "execution_count": null, |
| 114 | + "id": "9dd1d861", |
| 115 | + "metadata": {}, |
| 116 | + "outputs": [], |
| 117 | + "source": [] |
| 118 | + } |
| 119 | + ], |
| 120 | + "metadata": { |
| 121 | + "kernelspec": { |
| 122 | + "display_name": "Python 3 (ipykernel)", |
| 123 | + "language": "python", |
| 124 | + "name": "python3" |
| 125 | + }, |
| 126 | + "language_info": { |
| 127 | + "codemirror_mode": { |
| 128 | + "name": "ipython", |
| 129 | + "version": 3 |
| 130 | + }, |
| 131 | + "file_extension": ".py", |
| 132 | + "mimetype": "text/x-python", |
| 133 | + "name": "python", |
| 134 | + "nbconvert_exporter": "python", |
| 135 | + "pygments_lexer": "ipython3", |
| 136 | + "version": "3.8.10" |
| 137 | + } |
| 138 | + }, |
| 139 | + "nbformat": 4, |
| 140 | + "nbformat_minor": 5 |
| 141 | +} |
0 commit comments