DEV Community

Yaswanth K N
Yaswanth K N

Posted on

Lists in Python

  • The Purpose of this paper is to get required idea of methods of list in python in simplified way

  • List is a ordered collection of variables

  • lists can be non homogeneous

Methods:

ls.append('object')

This appends the object at end of list
Enter fullscreen mode Exit fullscreen mode

       ls.append('object') #Object will be added to list at the end

Enter fullscreen mode Exit fullscreen mode
  • ls.clear()

    clears the entire list and makes it empty

    
        ls.clear() # clears the entire list
    
    
  • ls.index()

    It will through error if object is not in list

    
        ls.index('object') #return the index value of 'object'
    
    
  • ls.count(arg)

    It will return Zero if element is not found in list

    
        freq = ls.count('objrct') # It will return the frequency of That Object in the list
    
    
  • ls.pop()

    This function removes last item of list and returns the item

    
        ls.pop() #Removes the last item of the list
    
    
  • ls.insert(index , 'object')

    
        ls.insert(index, 'object') #inserts object at given index
    
    
  • ls.sort()

    This function returns None and sorts the data inside list

    This will not work for non-homogeneous lists

    
        ls.sort() #sorts the items in Ascending order
    
    
  • ls.reverse()

    This function returns None and reverses the items inside list

    
        ls.reverse()
    
    
  • ls.extend()

    This function takes the iterable as argument and adds each item to the last

    
        ls = []
    
        ls.extend('iterable') # returns [i,t,e,r,a,b,l,e]
    
    
  • ls.copy()

    This function creates a copy of list

    
        ls = old_list.copy()
    
    
  • ls.remove()

    This Function takes the object inside the list as argument and removes it from the list

    If the Object is not present in list it will through error saying 'Object not list'

       ls.remove(5) #it will remove 5 from list
    
  • List as Stack

    list in python can be used as a stack, since it has both the methods .append() and .pop().

ls = []
#append
ls.append('a')
ls.append('b)
#pop
ls.pop() 
ls.pop() 
Enter fullscreen mode Exit fullscreen mode

References:

Top comments (0)