.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/basics/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_lists.py: =========== 1.4 lists =========== This lesson describes the concept of `list` in python. .. GENERATED FROM PYTHON SOURCE LINES 10-12 A list can be defined as a collection of objects or a container in which we hold different objects. .. GENERATED FROM PYTHON SOURCE LINES 12-15 .. code-block:: default mylist = [] .. GENERATED FROM PYTHON SOURCE LINES 16-18 Above we defined an empty list. How do we know that it is list? We can always check the type of object in python as below. .. GENERATED FROM PYTHON SOURCE LINES 18-21 .. code-block:: default print(type(mylist)) .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 22-23 We can also make a list which is not empty as below .. GENERATED FROM PYTHON SOURCE LINES 23-27 .. code-block:: default imperialists = ['Bush', 'Obama', 'Trump', 'Zuckerberg', 'Bezos', 'coca cola'] print(type(imperialists)) .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 28-30 In above-mentioned list, all of its (6) elements are strings. However, the elements/members a list are not required to be of same `type`. .. GENERATED FROM PYTHON SOURCE LINES 32-35 .. code-block:: default imperialists = ["Bush", {"years": 8}, 2000, (2000, 2008)] print(type(imperialists)) .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 36-43 In above list, the first member is ``string``, the second member is ``dictionary``, the third member is ``integer`` and the fourth member is a ``tuple``. We will study about string, dictionary, integer and tuple in upcoming lessons. There are two ways to convert a python object into list * using ``[]`` * usnig ``list`` function .. GENERATED FROM PYTHON SOURCE LINES 45-50 .. code-block:: default a = 1,2 print(type(a)) a_as_list_using_slice_op = [a] print(type(a_as_list_using_slice_op)) .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 51-54 .. code-block:: default a_as_list_using_list_fn = list(a) print(type(a_as_list_using_list_fn)) .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 55-58 However there is a major difference in these two. ``[]`` converts the whole object *as it is* into a list. On the other hand, ``list`` function is more like `element wise` operator. This can be verified by printing the converted lists created above. .. GENERATED FROM PYTHON SOURCE LINES 58-62 .. code-block:: default print(a_as_list_using_slice_op) print(a_as_list_using_list_fn) .. rst-class:: sphx-glr-script-out .. code-block:: none [(1, 2)] [1, 2] .. GENERATED FROM PYTHON SOURCE LINES 63-65 `a_as_list_using_list_fn` is a list with two members while `a_as_list_using_slice_op` is a list with only one member. This can be verified by checking the length of both lists. .. GENERATED FROM PYTHON SOURCE LINES 65-69 .. code-block:: default print(len(a_as_list_using_slice_op)) print(len(a_as_list_using_list_fn)) .. rst-class:: sphx-glr-script-out .. code-block:: none 1 2 .. GENERATED FROM PYTHON SOURCE LINES 70-77 Then length of list which is created using slice operator ``[]`` is always 1, while the length of list created using ``list`` function depends upon the number of values in the object. The slice operator is creating a list with one member and that one member is a tuple in this case. Spend some minutes on understanding the difference between these two ways of creating a list. Try with different objects and see the difference. .. GENERATED FROM PYTHON SOURCE LINES 79-81 **Question** Write code to prove the above statement. .. GENERATED FROM PYTHON SOURCE LINES 83-88 slicing a list =============== Since the list is a sequence, we can slice it as well. The slicing of a list can be done using the slice operator ``[]``. Consider the following list .. GENERATED FROM PYTHON SOURCE LINES 88-92 .. code-block:: default imperialists = ['Bush', 'Obama', 'Trump', 'Zuckerberg', 'Bezos', 'coca cola'] print(imperialists[0]) .. rst-class:: sphx-glr-script-out .. code-block:: none Bush .. GENERATED FROM PYTHON SOURCE LINES 93-95 .. code-block:: default print(imperialists[2]) .. rst-class:: sphx-glr-script-out .. code-block:: none Trump .. GENERATED FROM PYTHON SOURCE LINES 96-98 .. code-block:: default print(imperialists[-1]) .. rst-class:: sphx-glr-script-out .. code-block:: none coca cola .. GENERATED FROM PYTHON SOURCE LINES 99-102 The ``:`` operator/symbol is used to slice a list. The syntax is as follows ``list[start:stop:step]``. The default value of ``start`` is 0, of ``stop`` is length of list and of ``step`` is 1. .. GENERATED FROM PYTHON SOURCE LINES 102-105 .. code-block:: default print(imperialists[2:4]) .. rst-class:: sphx-glr-script-out .. code-block:: none ['Trump', 'Zuckerberg'] .. GENERATED FROM PYTHON SOURCE LINES 106-114 **Question:** What will be the output of the following code .. code-block:: python a = [1,5, 7,14] a[2] = 12 print(a) .. GENERATED FROM PYTHON SOURCE LINES 117-121 Operations on a list ===================== Once we have a list, we can perform different operations on it. Some of them are given below. .. GENERATED FROM PYTHON SOURCE LINES 123-125 append -------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 125-130 .. code-block:: default imperialists = ['Bush', 'Obama', 'Trump', 'Zuckerberg', 'Bezos', 'coca cola'] print(imperialists) .. rst-class:: sphx-glr-script-out .. code-block:: none ['Bush', 'Obama', 'Trump', 'Zuckerberg', 'Bezos', 'coca cola'] .. GENERATED FROM PYTHON SOURCE LINES 131-136 .. code-block:: default imperialists.append('clinton') print(imperialists) .. rst-class:: sphx-glr-script-out .. code-block:: none ['Bush', 'Obama', 'Trump', 'Zuckerberg', 'Bezos', 'coca cola', 'clinton'] .. GENERATED FROM PYTHON SOURCE LINES 137-138 `append` changes the original list and it itself returns `None`. .. GENERATED FROM PYTHON SOURCE LINES 140-144 .. code-block:: default new_imperialists = imperialists.append('netanyahu') print(new_imperialists) .. rst-class:: sphx-glr-script-out .. code-block:: none None .. GENERATED FROM PYTHON SOURCE LINES 145-149 .. code-block:: default print(imperialists) .. rst-class:: sphx-glr-script-out .. code-block:: none ['Bush', 'Obama', 'Trump', 'Zuckerberg', 'Bezos', 'coca cola', 'clinton', 'netanyahu'] .. GENERATED FROM PYTHON SOURCE LINES 150-152 If we add a similar but new element in list, then the list will have 2 such elements as its member. .. GENERATED FROM PYTHON SOURCE LINES 155-159 .. code-block:: default imperialists.append('netanyahu') print(imperialists) .. rst-class:: sphx-glr-script-out .. code-block:: none ['Bush', 'Obama', 'Trump', 'Zuckerberg', 'Bezos', 'coca cola', 'clinton', 'netanyahu', 'netanyahu'] .. GENERATED FROM PYTHON SOURCE LINES 160-168 **Question:** What will be the output of the following code? .. code-block:: python a = [1,5, 7,14] a.append(12) print(a[-1]) .. GENERATED FROM PYTHON SOURCE LINES 170-172 pop -------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 174-178 .. code-block:: default last_element = imperialists.pop(-1) print(imperialists) .. rst-class:: sphx-glr-script-out .. code-block:: none ['Bush', 'Obama', 'Trump', 'Zuckerberg', 'Bezos', 'coca cola', 'clinton', 'netanyahu'] .. GENERATED FROM PYTHON SOURCE LINES 179-182 .. code-block:: default print(last_element) .. rst-class:: sphx-glr-script-out .. code-block:: none netanyahu .. GENERATED FROM PYTHON SOURCE LINES 183-187 .. code-block:: default # uncomment following 1 line # imperialists.pop(8) .. GENERATED FROM PYTHON SOURCE LINES 188-191 .. code-block:: default imperialists.pop() .. rst-class:: sphx-glr-script-out .. code-block:: none 'netanyahu' .. GENERATED FROM PYTHON SOURCE LINES 192-195 .. code-block:: default print(imperialists) .. rst-class:: sphx-glr-script-out .. code-block:: none ['Bush', 'Obama', 'Trump', 'Zuckerberg', 'Bezos', 'coca cola', 'clinton'] .. GENERATED FROM PYTHON SOURCE LINES 196-200 .. code-block:: default # uncomment following 1 line # imperialists.pop('Bush') .. GENERATED FROM PYTHON SOURCE LINES 201-210 **Question:** What will be the output of the following code? .. code-block:: python a = [1,5, 7,14] a.pop(2) print(len(a)) # ?? a.pop(2) # ?? .. GENERATED FROM PYTHON SOURCE LINES 212-216 extend --------------------------------------------- If we want to add multiple elements to a list, using `append` will put a new list in the previous list .. GENERATED FROM PYTHON SOURCE LINES 218-224 .. code-block:: default imperialists = ['Bush', 'Obama', 'Trump', 'Zuckerberg', 'Bezos', 'coca cola', 'clinton'] uk_imperialists = ['churchil', 'Tony Blair', 'BBC'] imperialists.append(uk_imperialists) print(imperialists) .. rst-class:: sphx-glr-script-out .. code-block:: none ['Bush', 'Obama', 'Trump', 'Zuckerberg', 'Bezos', 'coca cola', 'clinton', ['churchil', 'Tony Blair', 'BBC']] .. GENERATED FROM PYTHON SOURCE LINES 225-230 .. code-block:: default imperialists = ['Bush', 'Obama', 'Trump', 'Zuckerberg', 'Bezos', 'coca cola', 'clinton'] imperialists.extend(uk_imperialists) print(imperialists) .. rst-class:: sphx-glr-script-out .. code-block:: none ['Bush', 'Obama', 'Trump', 'Zuckerberg', 'Bezos', 'coca cola', 'clinton', 'churchil', 'Tony Blair', 'BBC'] .. GENERATED FROM PYTHON SOURCE LINES 231-233 ``extend`` actually takes any sequence as input. It must not be a list. It can be a string or tuple. .. GENERATED FROM PYTHON SOURCE LINES 235-240 .. code-block:: default new_one = "Murdoch" imperialists.extend(new_one) print(imperialists) .. rst-class:: sphx-glr-script-out .. code-block:: none ['Bush', 'Obama', 'Trump', 'Zuckerberg', 'Bezos', 'coca cola', 'clinton', 'churchil', 'Tony Blair', 'BBC', 'M', 'u', 'r', 'd', 'o', 'c', 'h'] .. GENERATED FROM PYTHON SOURCE LINES 241-247 .. code-block:: default imperialists = ['Bush', 'Obama', 'Trump', 'clinton'] capitalists = ('Zuckerberg', 'Bezos', 'coca cola') imperialists.extend(capitalists) print(imperialists) .. rst-class:: sphx-glr-script-out .. code-block:: none ['Bush', 'Obama', 'Trump', 'clinton', 'Zuckerberg', 'Bezos', 'coca cola'] .. GENERATED FROM PYTHON SOURCE LINES 248-256 **Question:** What will be the output of the following code? .. code-block:: python a = [1,5, 7,14] b = [2, 3] print(len(a.extend(b))) # ? .. GENERATED FROM PYTHON SOURCE LINES 258-261 using ``+`` operator --------------------------------------------- We can also append lists using the `+` operator. .. GENERATED FROM PYTHON SOURCE LINES 263-267 .. code-block:: default media_houses = ['bbc', 'cnn', 'reuters', 'springer'] print(imperialists + media_houses) .. rst-class:: sphx-glr-script-out .. code-block:: none ['Bush', 'Obama', 'Trump', 'clinton', 'Zuckerberg', 'Bezos', 'coca cola', 'bbc', 'cnn', 'reuters', 'springer'] .. GENERATED FROM PYTHON SOURCE LINES 268-270 So when we use `+` operator between two lists, a new list is created which contains all the members of both lists. The original lists remain unchanged. .. GENERATED FROM PYTHON SOURCE LINES 270-273 .. code-block:: default print(imperialists) .. rst-class:: sphx-glr-script-out .. code-block:: none ['Bush', 'Obama', 'Trump', 'clinton', 'Zuckerberg', 'Bezos', 'coca cola'] .. GENERATED FROM PYTHON SOURCE LINES 274-278 .. code-block:: default imperialists = imperialists + media_houses print(imperialists) .. rst-class:: sphx-glr-script-out .. code-block:: none ['Bush', 'Obama', 'Trump', 'clinton', 'Zuckerberg', 'Bezos', 'coca cola', 'bbc', 'cnn', 'reuters', 'springer'] .. GENERATED FROM PYTHON SOURCE LINES 279-280 Above we are recreating the list `imperialists` by adding `media_houses` to it. .. GENERATED FROM PYTHON SOURCE LINES 280-285 .. code-block:: default morons = ['sam haris', 'richard dawkins', 'baghdadi', 'bin ladan'] imperialists += morons print(imperialists) .. rst-class:: sphx-glr-script-out .. code-block:: none ['Bush', 'Obama', 'Trump', 'clinton', 'Zuckerberg', 'Bezos', 'coca cola', 'bbc', 'cnn', 'reuters', 'springer', 'sam haris', 'richard dawkins', 'baghdadi', 'bin ladan'] .. GENERATED FROM PYTHON SOURCE LINES 286-288 `+=` operator means that the list on the left side of `+=` operator is updated by adding the list on the right side of `+=` operator. .. GENERATED FROM PYTHON SOURCE LINES 290-298 **Question:** What will be the output of the following code? .. code-block:: python a = [1,5, 7,14] b = [2, 3] print(len(a + b)) # ? .. GENERATED FROM PYTHON SOURCE LINES 301-303 remove --------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 305-309 .. code-block:: default imperialists.remove('springer') print(imperialists) .. rst-class:: sphx-glr-script-out .. code-block:: none ['Bush', 'Obama', 'Trump', 'clinton', 'Zuckerberg', 'Bezos', 'coca cola', 'bbc', 'cnn', 'reuters', 'sam haris', 'richard dawkins', 'baghdadi', 'bin ladan'] .. GENERATED FROM PYTHON SOURCE LINES 310-312 If we repeat the above operation, it will result in error because `springer` has already been removed from the list `imperialists`. .. GENERATED FROM PYTHON SOURCE LINES 314-319 .. code-block:: default # uncomment following 2 line # imperialists.remove('springer') # print(imperialists) .. GENERATED FROM PYTHON SOURCE LINES 320-323 insert -------------------------------------------- puts an the value before the index .. GENERATED FROM PYTHON SOURCE LINES 325-329 .. code-block:: default imperialists.insert(-1, 'DW') print(imperialists) .. rst-class:: sphx-glr-script-out .. code-block:: none ['Bush', 'Obama', 'Trump', 'clinton', 'Zuckerberg', 'Bezos', 'coca cola', 'bbc', 'cnn', 'reuters', 'sam haris', 'richard dawkins', 'baghdadi', 'DW', 'bin ladan'] .. GENERATED FROM PYTHON SOURCE LINES 330-332 Finding position/index of a member of a list ----------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 334-337 .. code-block:: default imperialists.index('bbc') .. rst-class:: sphx-glr-script-out .. code-block:: none 7 .. GENERATED FROM PYTHON SOURCE LINES 338-342 .. code-block:: default # uncomment following 1 line # imperialists.index('bbc', 8) .. GENERATED FROM PYTHON SOURCE LINES 343-347 .. code-block:: default # uncomment following 1 line # imperialists.index('bbc', 3, 6) .. GENERATED FROM PYTHON SOURCE LINES 348-349 if an element is present in a list twice, index of its first position is returned. .. GENERATED FROM PYTHON SOURCE LINES 351-356 .. code-block:: default last_value = imperialists[-1] imperialists.insert(2, last_value) print(imperialists) .. rst-class:: sphx-glr-script-out .. code-block:: none ['Bush', 'Obama', 'bin ladan', 'Trump', 'clinton', 'Zuckerberg', 'Bezos', 'coca cola', 'bbc', 'cnn', 'reuters', 'sam haris', 'richard dawkins', 'baghdadi', 'DW', 'bin ladan'] .. GENERATED FROM PYTHON SOURCE LINES 357-360 .. code-block:: default imperialists.index(last_value) .. rst-class:: sphx-glr-script-out .. code-block:: none 2 .. GENERATED FROM PYTHON SOURCE LINES 361-369 **Question:** What will be the output of the following code? .. code-block:: python a = [1,5, 7,14] a.insert(2, 12) print(a) # ? .. GENERATED FROM PYTHON SOURCE LINES 371-373 reverse -------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 375-379 .. code-block:: default imperialists = ['bbc', 'cnn', 'reuters', 'springer', 'voa'] imperialists.reverse() .. GENERATED FROM PYTHON SOURCE LINES 380-381 The function does not return anything itself but the original list is reversed. .. GENERATED FROM PYTHON SOURCE LINES 383-386 .. code-block:: default print(imperialists) .. rst-class:: sphx-glr-script-out .. code-block:: none ['voa', 'springer', 'reuters', 'cnn', 'bbc'] .. GENERATED FROM PYTHON SOURCE LINES 387-395 **Question:** What will be the output of the following code? .. code-block:: python a = [1,5, 7,14] a.reverse() print(a) # ? .. GENERATED FROM PYTHON SOURCE LINES 397-404 sort ---------------------------- We can sort the list using the `sort` function. If the contents of the list strings, then the list will be sorted in lexicographical order. If the contents are numbers, then the list will be sorted in ascending order. ############################################# .. GENERATED FROM PYTHON SOURCE LINES 404-407 .. code-block:: default print(imperialists) .. rst-class:: sphx-glr-script-out .. code-block:: none ['voa', 'springer', 'reuters', 'cnn', 'bbc'] .. GENERATED FROM PYTHON SOURCE LINES 408-409 The function does not return anything itself but the original list is sorted. .. GENERATED FROM PYTHON SOURCE LINES 409-412 .. code-block:: default imperialists.sort() .. GENERATED FROM PYTHON SOURCE LINES 413-416 .. code-block:: default print(imperialists) .. rst-class:: sphx-glr-script-out .. code-block:: none ['bbc', 'cnn', 'reuters', 'springer', 'voa'] .. GENERATED FROM PYTHON SOURCE LINES 417-421 .. code-block:: default imperialists.sort(reverse=True) print(imperialists) .. rst-class:: sphx-glr-script-out .. code-block:: none ['voa', 'springer', 'reuters', 'cnn', 'bbc'] .. GENERATED FROM PYTHON SOURCE LINES 422-427 .. code-block:: default numbers = [4,3,6,2] numbers.sort() print(numbers) .. rst-class:: sphx-glr-script-out .. code-block:: none [2, 3, 4, 6] .. GENERATED FROM PYTHON SOURCE LINES 428-433 .. code-block:: default # uncomment following 2 line # imperialists = ['bbc', 1, 'cnn', 3, 'voa', 2] # imperialists.sort() .. GENERATED FROM PYTHON SOURCE LINES 434-442 **Question** What will be the output of the following code .. code-block:: python x = [1,2] y = [3,4, 5] print(len(x + y)) .. GENERATED FROM PYTHON SOURCE LINES 444-446 ``*`` --------- .. GENERATED FROM PYTHON SOURCE LINES 446-449 .. code-block:: default a = ['Najaf'] print(a * 3) .. rst-class:: sphx-glr-script-out .. code-block:: none ['Najaf', 'Najaf', 'Najaf'] .. GENERATED FROM PYTHON SOURCE LINES 450-454 .. code-block:: default a = ['Najaf', '->', 'Karbala'] print(a * 3) .. rst-class:: sphx-glr-script-out .. code-block:: none ['Najaf', '->', 'Karbala', 'Najaf', '->', 'Karbala', 'Najaf', '->', 'Karbala'] .. GENERATED FROM PYTHON SOURCE LINES 455-463 Notes ============================================ I have been using the word function for ``append``, ``sort`` etc. However, in Object-Oriented Programming, it can be seen that they are actually called `methods`. There are a lot more powerful list manipulations which can be done by combining conditional and looping statements. We will come back to them once looping and conditioning statements are covered. .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.014 seconds) .. _sphx_glr_download_auto_examples_basics_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/lists.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: lists.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: lists.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_