.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/basics/copying_lists.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_basics_copying_lists.py: =================== 1.5 copying lists =================== This lessor describes how to copy a list in Python. .. important:: This lesson is still under development. .. GENERATED FROM PYTHON SOURCE LINES 14-15 Suppose we have a list of countries .. GENERATED FROM PYTHON SOURCE LINES 15-23 .. code-block:: default countries1 = ["Iraq", "Iran", "Lebanon"] countries2 = countries1 print(countries1) print(countries2) .. rst-class:: sphx-glr-script-out .. code-block:: none ['Iraq', 'Iran', 'Lebanon'] ['Iraq', 'Iran', 'Lebanon'] .. GENERATED FROM PYTHON SOURCE LINES 24-27 Above we have created a new list `countries2` and assigned the list `countries1` to it. Now both the lists `countries1` and `countries2` are same. We can check this by checking their memory address. .. GENERATED FROM PYTHON SOURCE LINES 27-30 .. code-block:: default print(id(countries1),id(countries2)) .. rst-class:: sphx-glr-script-out .. code-block:: none 139721349008256 139721349008256 .. GENERATED FROM PYTHON SOURCE LINES 31-36 We can see, both the lists `countries1` and `countries2` point to same object in memory. This means, even though we have two list variables, in reality we have only one object. What will happen if we redfine the list `countries2`? .. GENERATED FROM PYTHON SOURCE LINES 36-42 .. code-block:: default countries2 = ["Qatar", "Malaysia", "Turkey"] print(countries1) print(countries2) .. rst-class:: sphx-glr-script-out .. code-block:: none ['Iraq', 'Iran', 'Lebanon'] ['Qatar', 'Malaysia', 'Turkey'] .. GENERATED FROM PYTHON SOURCE LINES 43-46 .. code-block:: default print(id(countries1),id(countries2)) .. rst-class:: sphx-glr-script-out .. code-block:: none 139721349008256 139721348554496 .. GENERATED FROM PYTHON SOURCE LINES 47-51 Now `countries1` and `countries2` are different objects. So the `countries2` list became different from `countries1` when we assigned a different object to it. What happens when we change the contents of list .. GENERATED FROM PYTHON SOURCE LINES 53-63 .. code-block:: default countries1 = ["Pakistan", "Iran", "Turkey"] countries2 = countries1 countries2[2] = "Syria" print(countries1) print(countries2) .. rst-class:: sphx-glr-script-out .. code-block:: none ['Pakistan', 'Iran', 'Syria'] ['Pakistan', 'Iran', 'Syria'] .. GENERATED FROM PYTHON SOURCE LINES 64-68 Even though we only changed `countries2`, the list `countries1` changed automatically. This is because, we did't have two lists to begin with. We have one list with two names. What we did was `in place` change in `countries2` list and did not assign a new object to `countries2`, so the the name `countries2` still points to the same object as `countries1`. .. GENERATED FROM PYTHON SOURCE LINES 70-81 **Question:** What will be the output of the following code? .. code-block:: python nasalkush_countries = ["us", "uk", "germany"] istemari_countries = nasalkush_countries nasalkush_countries.extend(["france"]) print(istemari_countries) .. GENERATED FROM PYTHON SOURCE LINES 83-86 Copying using slicing ---------------------- The above problem can be avoided by using the slice operator .. GENERATED FROM PYTHON SOURCE LINES 88-97 .. code-block:: default countries1 = ["Pakistan", "Iran", "Turkey"] countries2 = countries1[:] countries2[2] = 'Syria' print(countries1) print(countries2) .. rst-class:: sphx-glr-script-out .. code-block:: none ['Pakistan', 'Iran', 'Turkey'] ['Pakistan', 'Iran', 'Syria'] .. GENERATED FROM PYTHON SOURCE LINES 98-101 .. code-block:: default print(id(countries1), id(countries2)) .. rst-class:: sphx-glr-script-out .. code-block:: none 139721348995776 139721349003520 .. GENERATED FROM PYTHON SOURCE LINES 102-105 Now `countries1` and `countries2` are not same objects. %% md The same can be achieved by using `copy` method of `list` object. .. GENERATED FROM PYTHON SOURCE LINES 107-115 .. code-block:: default countries1 = ["Pakistan", "Iran", "Turkey"] countries2 = countries1.copy() print(id(countries1), id(countries2)) # What about sublists in a list? .. rst-class:: sphx-glr-script-out .. code-block:: none 139721349010176 139721348995776 .. GENERATED FROM PYTHON SOURCE LINES 116-124 .. code-block:: default countries1 = ["Pakistan", "Iran", "Turkey",["Qatar", "Malaysia"]] countries2 = countries1[:] print(countries1) print(countries2) .. rst-class:: sphx-glr-script-out .. code-block:: none ['Pakistan', 'Iran', 'Turkey', ['Qatar', 'Malaysia']] ['Pakistan', 'Iran', 'Turkey', ['Qatar', 'Malaysia']] .. GENERATED FROM PYTHON SOURCE LINES 125-131 .. code-block:: default countries2[0] = "Syria" print(countries1) print(countries2) .. rst-class:: sphx-glr-script-out .. code-block:: none ['Pakistan', 'Iran', 'Turkey', ['Qatar', 'Malaysia']] ['Syria', 'Iran', 'Turkey', ['Qatar', 'Malaysia']] .. GENERATED FROM PYTHON SOURCE LINES 132-138 .. code-block:: default countries2[3][1] = "Iraq" print(countries1) print(countries2) .. rst-class:: sphx-glr-script-out .. code-block:: none ['Pakistan', 'Iran', 'Turkey', ['Qatar', 'Iraq']] ['Syria', 'Iran', 'Turkey', ['Qatar', 'Iraq']] .. GENERATED FROM PYTHON SOURCE LINES 139-142 So again `countries1` is changed even though we changed only `countries2` list. This is because when we copy a list containing sublists, the sublists are not copied but their reference is copied. .. GENERATED FROM PYTHON SOURCE LINES 145-146 We can check that the sublists in both lists (countries1 and countries1) are same objects. .. GENERATED FROM PYTHON SOURCE LINES 146-148 .. code-block:: default print(id(countries1[3]), id(countries2[3])) .. rst-class:: sphx-glr-script-out .. code-block:: none 139721349010688 139721349010688 .. GENERATED FROM PYTHON SOURCE LINES 149-150 The same can problem arisis when use `copy` method of `list` object. .. GENERATED FROM PYTHON SOURCE LINES 152-158 .. code-block:: default countries1 = ["Pakistan", "Iran", "Turkey",["Qatar", "Malaysia"]] countries2 = countries1.copy() print(id(countries1), id(countries2)) .. rst-class:: sphx-glr-script-out .. code-block:: none 139721349009600 139721349010752 .. GENERATED FROM PYTHON SOURCE LINES 159-166 .. code-block:: default countries2[3][1] = "Iraq" print(countries1) print(countries2) .. rst-class:: sphx-glr-script-out .. code-block:: none ['Pakistan', 'Iran', 'Turkey', ['Qatar', 'Iraq']] ['Pakistan', 'Iran', 'Turkey', ['Qatar', 'Iraq']] .. GENERATED FROM PYTHON SOURCE LINES 167-170 .. code-block:: default print(id(countries1[3]), id(countries2[3])) .. rst-class:: sphx-glr-script-out .. code-block:: none 139721349011392 139721349011392 .. GENERATED FROM PYTHON SOURCE LINES 171-174 Using ``list`` function ------------------------ The ``list`` function converts a sequence into list, if it is not already a list .. GENERATED FROM PYTHON SOURCE LINES 176-182 .. code-block:: default countries1 = ["Pakistan", "Iran", "Turkey",["Qatar", "Malaysia"]] countries2 = list(countries1) print(id(countries1), id(countries2)) .. rst-class:: sphx-glr-script-out .. code-block:: none 139721348995776 139721348824576 .. GENERATED FROM PYTHON SOURCE LINES 183-186 .. code-block:: default print(id(countries1[3]), id(countries2[3])) .. rst-class:: sphx-glr-script-out .. code-block:: none 139721349012096 139721349012096 .. GENERATED FROM PYTHON SOURCE LINES 187-189 using copy module ------------------ .. GENERATED FROM PYTHON SOURCE LINES 191-199 .. code-block:: default from copy import copy countries1 = ["Pakistan", "Iran", "Turkey",["Qatar", "Malaysia"]] countries2 = copy(countries1) print(id(countries1), id(countries2)) .. rst-class:: sphx-glr-script-out .. code-block:: none 139721348554432 139721348995776 .. GENERATED FROM PYTHON SOURCE LINES 200-203 .. code-block:: default print(id(countries1[3]), id(countries2[3])) .. rst-class:: sphx-glr-script-out .. code-block:: none 139721348526400 139721348526400 .. GENERATED FROM PYTHON SOURCE LINES 204-213 .. code-block:: default from copy import deepcopy countries1 = ["Pakistan", "Iran", "Turkey",["Qatar", "Malaysia"]] countries2 = deepcopy(countries1) print(id(countries1), id(countries2)) .. rst-class:: sphx-glr-script-out .. code-block:: none 139721349012288 139721348554432 .. GENERATED FROM PYTHON SOURCE LINES 214-216 Now `countries1` and `countries2` are different and also the sublists in both lists are different now. .. GENERATED FROM PYTHON SOURCE LINES 218-221 .. code-block:: default print(id(countries1[3]), id(countries2[3])) .. rst-class:: sphx-glr-script-out .. code-block:: none 139721350848704 139721352060480 .. GENERATED FROM PYTHON SOURCE LINES 222-223 So if we make change in one sublist, the sublist in other list will not be affected. .. GENERATED FROM PYTHON SOURCE LINES 225-232 .. code-block:: default countries2[3][1] = "Iraq" print(countries1) print(countries2) .. rst-class:: sphx-glr-script-out .. code-block:: none ['Pakistan', 'Iran', 'Turkey', ['Qatar', 'Malaysia']] ['Pakistan', 'Iran', 'Turkey', ['Qatar', 'Iraq']] .. GENERATED FROM PYTHON SOURCE LINES 233-234 but the strings in them are not copied so they still have same id. .. GENERATED FROM PYTHON SOURCE LINES 236-239 .. code-block:: default print(id(countries1[0]), id(countries2[0])) .. rst-class:: sphx-glr-script-out .. code-block:: none 139721349009840 139721349009840 .. GENERATED FROM PYTHON SOURCE LINES 240-243 So ``deepcopy`` method from `copy` module is the safest way to copy a list when it contain sublists but it is also the slowest one among others. But what if the two sublists in a list are themselves same objects? .. GENERATED FROM PYTHON SOURCE LINES 245-254 .. code-block:: default countries1 = ["Pakistan", "Iran"] b = [countries1,countries1] # there's only 1 object countries1 c = deepcopy(b) # check the result c[0] is countries1 # return False, a new object a' is created .. rst-class:: sphx-glr-script-out .. code-block:: none False .. GENERATED FROM PYTHON SOURCE LINES 255-258 .. code-block:: default c[0] is c[1] .. rst-class:: sphx-glr-script-out .. code-block:: none True .. GENERATED FROM PYTHON SOURCE LINES 259-261 In such a scenario, copying with nested for loops is the safest way. `for loops` will be described later. .. GENERATED FROM PYTHON SOURCE LINES 263-269 .. code-block:: default countries1 = ["Pakistan", "Iran"] b = [countries1,countries1] # there's only 1 object countries1 c = [[i for i in row] for row in b] .. GENERATED FROM PYTHON SOURCE LINES 270-273 .. code-block:: default c[0] is countries1 .. rst-class:: sphx-glr-script-out .. code-block:: none False .. GENERATED FROM PYTHON SOURCE LINES 274-277 .. code-block:: default c[0] is c[1] .. rst-class:: sphx-glr-script-out .. code-block:: none False .. GENERATED FROM PYTHON SOURCE LINES 278-281 If you don't understand the above code, don't worry. We will discuss it later. For now, just remember that if you have a list containing lists, then `deepcopy` method will not work as expected. .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.010 seconds) .. _sphx_glr_download_auto_examples_basics_copying_lists.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/AtrCheema/python-seekho/master?urlpath=lab/tree/notebooks/auto_examples/basics/copying_lists.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: copying_lists.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: copying_lists.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_