Page 297 - Computer Science Class 11 Without Functions
P. 297

>>> 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)
            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.
            ●  lst.reverse(): The method reverse() reverses the order of the elements in the list. For example,

             >>> names = ['Aryan', 'Anthony', 'Samantha', 'Sunpreet', 'Venkatesh']
             >>> names.reverse()
             >>> names
                 ['Venkatesh', 'Sunpreet', 'Samantha', 'Anthony', 'Aryan']
            ●  lst.sort(): The method arranges the elements of the list in ascending order.

            Example:
             >>> names.sort()
             >>> names
                 ['Anthony', 'Aryan', 'Samantha', 'Sunpreet', 'Venkatesh']
            To sort the elements in a list in descending order, we set the argument reverse = True, as shown below:

             >>> names = ['Aryan', 'Anthony', 'Samantha', 'Sunpreet', 'Venkatesh']
             >>> names.sort(reverse = True)
             >>> names
                 ['Venkatesh', 'Sunpreet', 'Samantha', 'Aryan', 'Anthony']
            ●  lst.remove(element): The method remove(element) searches for the first instance of the element in
              the list lst and removes it. If an element is not found in lst, the method remove throws ValueError.
             >>> lst = [10, 20, 30, 10, 50, 20, 60, 20, 30, 55]
             >>> lst.remove(20)
             >>> lst
                 [10, 30, 10, 50, 20, 60, 20, 30, 55]
             >>> lst.remove(66)
                 Traceback (most recent call last):
                   File "<pyshell#7>", line 1, in <module>
                     lst.remove(66)
                 ValueError: list.remove(x): x not in list
            ●  lst.pop(index): The method removes the element from the specified index and returns the element removed
              from the list. If the index is omitted, the rightmost element is returned. For example,

             >>> lst = [10, 20, 30, 10, 50, 20, 60, 20, 30, 55]
             >>> lst.pop(3)
                 10
             >>> lst
                 [10, 20, 30, 50, 20, 60, 20, 30, 55]

                                                                                                Lists and Tuples  295
   292   293   294   295   296   297   298   299   300   301   302