Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 151 - 2620_Birla Open Mind C-8
P. 151

    copy( )    Returns a copy of the list
                              clear( )   Removes all the elements from the list and returns an empty list

                              sort( )    Sorts all the elements in the list
                              reverse( )  Reverses the order of the elements in the list

                     Program 8: To use the built-in methods on the list
                         Program8.py
                      File  Edit  Format   Run   Options   Window     Help

                      list1=[13, 25, 41, 63, 82]
                      list1.append(19)
                      print(list1)
                      list1.extend([26, 43, 55])
                      print(list1)
                      list1.insert(1,302)
                      print(list1)
                      list1.remove(63)
                      print(list1)
                      print(list1.index(13))
                      print(list1.count(41))
                      list1.reverse()
                      print(list1)
                      list1.sort()
                      print(list1)

                     On running the above program, you will get the following output:

                         Output

                      [13, 25, 41, 63, 82, 19]
                      [13, 25, 41, 63, 82, 19, 26, 43, 55]
                      [13, 302, 25, 41, 63, 82, 19, 26, 43, 55]
                      [13, 302, 25, 41, 82, 19, 26, 43, 55]
                      0
                      1
                      [55, 43, 26, 19, 82, 41, 25, 302, 13]
                      [13, 19, 25, 26, 41, 43, 55, 82, 302]

                     Program 9: To use pop, copy, and clear built-in methods on the list
                         Program9.py

                      File  Edit  Format   Run   Options   Window     Help

                      list1 = [1, 2, 3, 4, 5]
                      list1.pop(3)  # Remove and return the element at index 3.
                      print(list1)
                      list1.copy()  # Create a shallow copy of the list
                      print(list1)
                      list1.clear()  # Remove all elements from the list
                      print(list1)

                                                                                                         #List in Python 149

| 146 147 148 149 150 151 152 153 154 155 156 |