.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "11_demos\python\demo_listTupleDict.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_11_demos_python_demo_listTupleDict.py: Lists, tuples, dictionaries ============================== Lists .. GENERATED FROM PYTHON SOURCE LINES 6-14 .. code-block:: default list1 = ["physics", "chemistry", 1997, 2000] list2 = [1, 2, 3, 4, 5] list3 = ["a", "b", "c", "d"] print("list1[0]: ", list1[0]) print("list2[1:5]: ", list2[1:5]) .. rst-class:: sphx-glr-script-out .. code-block:: none list1[0]: physics list2[1:5]: [2, 3, 4, 5] .. GENERATED FROM PYTHON SOURCE LINES 16-17 Updating lists .. GENERATED FROM PYTHON SOURCE LINES 17-20 .. code-block:: default list1[2] = 2001 print("New value available at index 2 : ", list1[2]) .. rst-class:: sphx-glr-script-out .. code-block:: none New value available at index 2 : 2001 .. GENERATED FROM PYTHON SOURCE LINES 21-22 Delete list item .. GENERATED FROM PYTHON SOURCE LINES 22-25 .. code-block:: default del list1[2] print("After deleting value at index 2 : ", list1) .. rst-class:: sphx-glr-script-out .. code-block:: none After deleting value at index 2 : ['physics', 'chemistry', 2000] .. GENERATED FROM PYTHON SOURCE LINES 26-27 List operations .. GENERATED FROM PYTHON SOURCE LINES 27-34 .. code-block:: default len(list1) # length of list list1 + list2 # concatenate list ["ho"] * 4 # repetition 2000 in list1 # membership for obj in list1: # iterations print(obj) .. rst-class:: sphx-glr-script-out .. code-block:: none physics chemistry 2000 .. GENERATED FROM PYTHON SOURCE LINES 35-36 List functions, methods .. GENERATED FROM PYTHON SOURCE LINES 36-46 .. code-block:: default list1.append("newVal") # append value list1.append(2000) list1.count(2000) # count how many times object occurs in list list1.index(2000) # lowest index in list object appears list1.insert(3, 2022) # insert object into list at index# list1.pop() # remove last object from list list1.remove(2022) # remove object from list list1.reverse() # reverse objects in list list2.sort() # sorts objects in list. Works only if list contains object of same dtype .. GENERATED FROM PYTHON SOURCE LINES 47-48 Tuples .. GENERATED FROM PYTHON SOURCE LINES 48-53 .. code-block:: default tup1 = ("physics", "chemistry", 1997, 2000) tup2 = (1, 2, 3, 4, 5, 6, 7) print("tup1[0]: ", tup1[0]) print("tup2[1:5]: ", tup2[1:5]) .. rst-class:: sphx-glr-script-out .. code-block:: none tup1[0]: physics tup2[1:5]: (2, 3, 4, 5) .. GENERATED FROM PYTHON SOURCE LINES 54-55 Updating tuples .. GENERATED FROM PYTHON SOURCE LINES 55-58 .. code-block:: default tup3 = tup1 + tup2 print(tup3) .. rst-class:: sphx-glr-script-out .. code-block:: none ('physics', 'chemistry', 1997, 2000, 1, 2, 3, 4, 5, 6, 7) .. GENERATED FROM PYTHON SOURCE LINES 59-60 Tuples operations .. GENERATED FROM PYTHON SOURCE LINES 60-67 .. code-block:: default len(tup1) # length of list tup1 + tup2 # concatenate list ("ho") * 4 # repetition 2000 in tup1 # membership for obj in tup1: # iterations print(obj) .. rst-class:: sphx-glr-script-out .. code-block:: none physics chemistry 1997 2000 .. GENERATED FROM PYTHON SOURCE LINES 68-69 Dictionary .. GENERATED FROM PYTHON SOURCE LINES 69-72 .. code-block:: default dict1 = {"Name": "Zara", "Age": 7, "Class": "First"} print("dict1['Name']: ", dict1["Name"]) .. rst-class:: sphx-glr-script-out .. code-block:: none dict1['Name']: Zara .. GENERATED FROM PYTHON SOURCE LINES 73-74 Updating dictionary .. GENERATED FROM PYTHON SOURCE LINES 74-80 .. code-block:: default dict1["Age"] = 8 # update existing entry dict1["School"] = "DPS School" # Add new entry print("dict1['Age']: ", dict1["Age"]) print("dict1['School']: ", dict1["School"]) .. rst-class:: sphx-glr-script-out .. code-block:: none dict1['Age']: 8 dict1['School']: DPS School .. GENERATED FROM PYTHON SOURCE LINES 81-82 Delete elements .. GENERATED FROM PYTHON SOURCE LINES 82-88 .. code-block:: default del dict1["Name"] # remove entry with key 'Name' print("dict1['Age']: ", dict1["Age"]) print("dict1['School']: ", dict1["School"]) dict1.clear() # remove all entries in dict del dict1 # delete entire dictionary .. rst-class:: sphx-glr-script-out .. code-block:: none dict1['Age']: 8 dict1['School']: DPS School .. GENERATED FROM PYTHON SOURCE LINES 89-90 Dictionary functions, methods .. GENERATED FROM PYTHON SOURCE LINES 90-97 .. code-block:: default dict1 = {"Name": "Zara", "Age": 7, "Class": "First"} dict1.copy() # returns shallow copy of dict dict.fromkeys(dict1) # new dict with keys from sequence and values set to value dict1.items() # returns list of dict (key, value) tuple pairs dict1.keys() # return list of keys for key, value in dict1.items(): # iterations print(key, value) .. rst-class:: sphx-glr-script-out .. code-block:: none Name Zara Age 7 Class First .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.012 seconds) .. _sphx_glr_download_11_demos_python_demo_listTupleDict.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demo_listTupleDict.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: demo_listTupleDict.ipynb `