Page 135 - Code_GPT_Class_8
P. 135

Program 4: To find the elements in the list using the index value

                     Program4.py
                  File  Edit  Format  Run   Options  Window    Help

                  list1=[13, 25, 41, 63, 82]
                                                                                  Output
                  print(list1[0])                                              13
                  print(list1[3])                                              63
                  print(list1[4])                                              82






                          Factbot


                   If a user tries to access a list element by using floating-point indexing, it will result in IndexError.




                 Negative Indexing

                 In negative indexing, the indexing starts from -1 which is the last elements of the list, the index of -2
                 refers to the second last element.

                 Program 5: To traverse a list by using negative indexing

                     Program5.py

                  File  Edit  Format  Run   Options  Window    Help
                  list1=[13, 25, 41, 63, 82]
                                                                                  Output
                  print(list1[-2])                                             63
                  print(list1[-1])                                             82
                  print(list1[-4])                                             25





                 Traversing Nested Lists

                 Nested list can be traversed by using the index operator.

                 Program 6: To traverse the nested lists

                     Program6.py
                  File  Edit  Format   Run   Options   Window     Help


                  list1=[2, [2002, "Orange", 65.5], [4002, "Orange Education", 46.5]]

                  print(list1[2][1])
                  print(list1[1][2])









                                                                                                   List in Python  133
   130   131   132   133   134   135   136   137   138   139   140