Python List | Overview of list data type built in methods

The list data type is built in data type in Python, it has several very useful methods. Some of the methods are explained in this post.
  • Get all methods available for list data type in Python
  • 
    my_list = [34, 56, 78, 43, 54, 67, 23]
    print(dir(my_list))
    
    =====Output=====
    ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
    
  • Appending elements in Python list
  • 
    # Appending elements in Python list
    my_list = [34, 56, 78, 43, 54, 67, 23]
    my_list.append(35)
    print(my_list)
    
    =====Output=====
    [34, 56, 78, 43, 54, 67, 23, 35]
    
    
  • Inserting elements at specific index in Python list
  • 
    # Inserting elements in Python list
    my_list = [34, 56, 78, 43, 54, 67, 23]
    my_list.insert(2, 999)
    print(my_list)   
    
    =====Output=====
    [34, 56, 999, 78, 43, 54, 67, 23]
    
  • Removing element from Python list
  • 
    # Removing elements from Python list
    my_list = [34, 56, 78, 43, 54, 67, 23]
    my_list.remove(34)
    print(my_list)
    
    =====Output=====
    [56, 78, 43, 54, 67, 23]
    
  • Extending list in Python with iterator
  • 
    # Extending list in Python with iterator
    my_list = [34, 56, 78, 43, 54, 67, 23]
    my_list_2 = [999, 555, 444, 999]
    my_list.extend(my_list_2)
    print(my_list)
    
    =====Output=====
    [34, 56, 78, 43, 54, 67, 23, 999, 555, 444, 999]
    
  • Getting number of occurrence of an element in Python list
  • 
    # Getting number of occurrence of an element in Python list
    my_list = [34, 56, 78, 43, 54, 67, 23]
    occurrence_count = my_list.count(78)
    print(occurrence_count)
    
    =====Output=====
    1
    
  • Removing element with "pop" method in Python list
  • 
    # Pop method in Python list 
    my_list = [34, 56, 78, 43, 54, 67, 23]
    my_list.pop(3)
    print(my_list)
    
    =====Output=====
    [34, 56, 78, 54, 67, 23]
    
  • Adding all the elements of Python list
  • 
    # Adding elements of Python list
    my_list = [34, 56, 78, 43, 54, 67, 23]
    list_sum = sum(my_list)
    print(list_sum)
    
    =====Output=====
    355
    
  • Sorting elements of Python list
  • 
    # Sorting elements in Python list
    my_list = [34, 56, 78, 43, 54, 67, 23] 
    sorted_list = sorted(my_list)
    print(sorted_list)
    
    =====Output=====
    [23, 34, 43, 54, 56, 67, 78]
    
    
  • Reversing a list in Python
  • 
    # Reversing a list in Python
    my_list = [34, 56, 78, 43, 54, 67, 23]
    reversed_list = my_list[::-1]
    print(reversed_list)
    
    =====Output=====
    [23, 67, 54, 43, 78, 56, 34]
    
  • Removing all elements from Python list
  • 
    #Removing all elements from Python list
    my_list = [34, 56, 78, 43, 54, 67, 23]
    my_list.clear()
    print(my_list) 
    
    =====Output=====
    []
    
  • Complete code
  • 
    my_list = [34, 56, 78, 43, 54, 67, 23]
    
    print(dir(my_list))
    
    # Appending elements in Python list
    my_list = [34, 56, 78, 43, 54, 67, 23]
    my_list.append(35)
    print(my_list)
    
    # Inserting elements in Python list
    my_list = [34, 56, 78, 43, 54, 67, 23]
    my_list.insert(2, 999)
    print(my_list)
    
    
    # Removing elements from Python list
    my_list = [34, 56, 78, 43, 54, 67, 23]
    my_list.remove(34)
    print(my_list)
    
    
    # Extending list in Python with iterator
    my_list = [34, 56, 78, 43, 54, 67, 23]
    my_list_2 = [999, 555, 444, 999]
    my_list.extend(my_list_2)
    print(my_list)
    
    # Getting number of occurrence of an element in list
    my_list = [34, 56, 78, 43, 54, 67, 23]
    occurrence_count = my_list.count(78)
    print(occurrence_count)
    
    # Pop method in Python list 
    my_list = [34, 56, 78, 43, 54, 67, 23]
    my_list.pop(3)
    print(my_list)
    
    # Adding elements of Python list
    my_list = [34, 56, 78, 43, 54, 67, 23]
    list_sum = sum(my_list)
    print(list_sum)
    
    # Sorting elements in Python list
    my_list = [34, 56, 78, 43, 54, 67, 23] 
    sorted_list = sorted(my_list)
    print(sorted_list)
    
    
    
    # Reversing a list in Python
    my_list = [34, 56, 78, 43, 54, 67, 23]
    reversed_list = my_list[::-1]
    print(reversed_list)
    
    
    #Removing all elements from Python list
    my_list = [34, 56, 78, 43, 54, 67, 23]
    my_list.clear()
    print(my_list)
    
    =====Output=====
    ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
    [34, 56, 78, 43, 54, 67, 23, 35]
    [34, 56, 999, 78, 43, 54, 67, 23]
    [56, 78, 43, 54, 67, 23]
    [34, 56, 78, 43, 54, 67, 23, 999, 555, 444, 999]
    1
    [34, 56, 78, 54, 67, 23]
    355
    [23, 34, 43, 54, 56, 67, 78]
    [23, 67, 54, 43, 78, 56, 34]
    []
    
    

    Categories: Python