Lists and built-in methods II



👈Previous Topic


Hello folks !!!! 
Today's content is continuation of yesterday's content. So please make sure you read about Lists in python before you come into this content.

6. pop() :

  • This method is used to remove an item from the list by mentioning its index.
  • The syntax is List_name.pop(index_number).   
For example:
    lists-pop()

    Here '2' does not refer to element '2', but to the index number '2'. That is why the element '3' is removed(Index in lists start from 0).

    7. max() and min() :

    • These methods are used to find the normalst and smallest elemnts in the list. 
    • The syntax is max(List_name) and min(List_name).
    • How about trying an example on your own...!?

    8. len() :
    • This method is used to find the length of the lists.
    • The syntax is len(List_name).
    • How about trying an example on your own...!?

    9. sum() :

    • When there is a list of integers or floats, we can find their sum easily using this method.
    • The syntax is sum(List_name).
    • Note : In case of floating point numbers, if the numbers after decimal point are huge, we can round them off using a method. The method is round(variable,no.of.digits). This method can be used for normal variables. It is not mandatory that you should use it with sum() or lists only. For example:
    lists-sum()

    Here the round() method is used. The first argument passed to it is the sum calculated out the elements in the list and the second argument denotes , to how many digits the answer should be rounded off.

    10. sort()and sorted() :

    • This method returns a list that is sorted either in ascending or descending order.
    • The output of this method need not be assigned to a list variable to use it. It sorts the original list.
    • It takes an optional argument which is used for sorting in reverse order. By default it sorts the list in ascending order.
    • The sorted() method is also similar the sort() method. The difference is in syntax and this requires the output to be stored in a list variable for further manipulations.
    • The syntax for the methods are as follows : List_name.sort(), sorted(List_name)
    For example: Ascending order code is as follows.
      lists-sort()

      To sort in descending order :
        lists-sort() descending
        • This method can be applied on alphabets also.

        11. reverse() :

        • This simply reverses the given list.
        • Its syntax is List_name.reverse(). The output need not be stored in any other variable. It makes changes to the original list itself.
        • For example :
        lists-reverse()

        12. index() :

        • This method is used to get the index number of a specific element in the list.
        • If the number is repeated more than once, it returns the index at which it makes its first appearance.
        • If you try to get the index of an element that is not in the list, you will get an error message. For example:
        lists-index()

        13. count() :

        • This method is used to count the no.of.occurrences of the element passed to the function in a list.
        • When the items are repeated in a list, their frequency is given by this method.
        • The syntax is List_name.count(item).
        • For example : let the list be li=[1,1,4,4,6,2,2,2,5,7]. Now if you write li.count(2) you will get the output 3. 
        • If you need to count the frequency of characters or words in the list, then write it in quotes.

        These all are the important methods that are associated with the lists. Try out all these methods. Ping me if you have doubts.

        Next Topic👉

        Comments