Page 256 - Ai_V1.0_Class9
P. 256

#adding a list at the end of another list              ['cat', 'dog', 'rabbit', ['tiger', 'lion']]
                 pets = ['cat', 'dog', 'rabbit']
                 wild = ['tiger', 'lion']

                 pets.append(wild)
                 print(pets)
              The extend( ) Function

              The extend() function is used to append multiple values at a time in a list. The syntax of the extend()
              function is:

                 list.extend(iterable value)

              where iterable value can be a list. For example,
                                          Commands                             Output

                                n=[1,2,3,4]                     [1, 2, 3, 4, 5]
                                m=[5]
                                n.extend(m)
                                print(n)
                                pets  =  ['cat',  'dog',  ['cat', 'dog', 'rabbit', 'tiger', 'lion']
                                'rabbit']
                                wild=['tiger', 'lion']

                                pets.extend(wild)
                                print(pets)

              The insert( ) Function

              The insert() function is used to add a single value at a specific position in an existing list. The length of the
              list will increase by one. The syntax of the insert() function is:

                list.insert(index, value)
              For example,

                                               Commands                         Output

                                    L = [10, 20, 30]                   [10, 40, 20, 30]
                                    L.insert(1, 40)
                                    print(L)
                                    L = [10, 20, 30]                   ['abc', 10, 20, 30]
                                    L.insert(0, "abc")
                                    print(L)
                                    L = [10, 20, 30]                   [10, 20, 'xyz', 30]
                                    a = "xyz"
                                    L.insert(2, a)
                                    print(L)

                                    L = [10, 20, 30]                   [10, ['a', 'b'], 20, 30]
                                    L.insert(1, ["a", "b"])
                                    print(L)
                    254     Artificial Intelligence Play (Ver 1.0)-IX
   251   252   253   254   255   256   257   258   259   260   261