Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 101 - 2612_Digimind
P. 101

    The extend( ) Function
                     Using the append( ) function, we can only add one element at a time. For adding more than one

                     element, we use the extend( ) function. Syntax of using extend() function is:
                         list_name.extend(iterable value)
                     Program 17: To add elements to an existing list using extend() function

                         Program17.py

                      File  Edit  Format   Run    Options   Window    Help
                      a=[1,2,3,4,5]
                      a.extend([6,7,8,9])
                      print (a)

                         Output
                      [1, 2, 3, 4, 5, 6, 7, 8, 9]

                     THE del STATEMENT

                     We use the del statement to remove an element or a slice of element from a list.
                     Syntax for using del statement is:

                         del list_name[start : stop : step]
                     Program 18: To delete elements from the list

                         Program18.py
                      File  Edit  Format   Run    Options   Window    Help

                      a=[1,2,3,4,5,6,7]
                      del a[1:3:2]
                      print (a)

                         Output
                      [1, 3, 4, 5, 6, 7]

                     Program 19: To delete the whole list

                         Program19.py
                      File  Edit  Format   Run    Options   Window    Help
                      a=[1,2,3,4,5,6,7]
                      del a

                     The whole list will be deleted.

                                                                                       Functions, String and List in Python  99

| 96 97 98 99 100 101 102 103 104 105 106 |