Page 193 - Information_Practice_Fliipbook_Class11
P. 193

●  lst.extend(obj): The method extend() inserts the elements in obj, passed as an argument, at the end
              of the elements of the list lst. Note that obj may not necessarily be a list and could as well be a tuple, set, or
              dictionary.
             Example:

             >>> intList = [10, 20, 30]
             >>> intList.extend([40,50])
             >>> intList
                 [10, 20, 30, 40, 50]


                     Assuming that lst is a list, what is the difference between the operations?
                            lst.extend([40,50]) and lst + [40, 50]


              >>>> 1st = [1,2,3]
             >>> 1st.extend([4,5])
             >>> 1st
                 [1,2,3,4,5]
             >>> 1st = [1,2,3]
             >>> 1st + [4,5]
                 [1,2,3,4,5]
             >>> 1st
                 [1,2,3]
            ●  lst.index(elem): The method index() returns the index of the first occurrence of the element elem in the
              list. If elem does not appear in the list lst, it throws a ValueError, for example,

             >>> names = ['Aryan', 'Anthony', 'Samantha', 'Sunpreet', 'Venkatesh']
             >>> names.index('Samantha')
                 2
             >>> name = 'Arjun'
             >>> names.index(name)
                 Traceback (most recent call last):
                   File '<pyshell#3>', line 1, in <module>
                     names.index(name)
                 ValueError: 'Arjun' is not in list
            Note: To avoid ValueError, we can use the membership operator to check the membership of an element.
                 name = 'Arjun'
             >>> if name in names:
             ...      names.index(name)
             ... else:
             ...     print(name, "doesn't exist in the list")
                 Arjun doesn't exist in the list
            ●  lst.count(element): The method count() returns the number of times an element elem appears in a list.
              For example,

             >>> fruits = ['apple', 'banana', 'orange', 'banana', 'kiwi']
             >>> countOccurrence = fruits.count('banana')
             >>> print(countOccurrence)
                Sample Output:
                 2
            In this example, we have a list of fruits that contains multiple occurrences of the string 'banana'. We use the
            count() method to count the number of times 'banana' appears in the list and store the result in the variable
            countOccurrence. The print() function then outputs the value of countOccurrence, which is 2.
            Note that the count() method only counts the occurrences of a single element in the list. If you want to count the
            occurrences of multiple elements in the list, you will need to call the count() method separately for each element.
            Also note that the count() method returns 0 if the element is not present in the list.


                                                                                                   Python Lists  179
   188   189   190   191   192   193   194   195   196   197   198