Programming Tutorials

  • View all tutorials
  • Common list methods in Python
    Below are some list functions that are commonly required in order to complete Coderbyte challenges. You can view a longer list of Python list functions here.

    append and insert

    Add an element to the end of the list and add an element into a specific index in the list.
    li = [1, 2, 3]
    
    li.append(4) # [1, 2, 3, 4]
    li.insert(0, -100) # [-100, 1, 2, 3, 4]
    

    pop and remove

    Remove the last element from the list or remove a specific element from the list.
    li = [1, 2, 3, 4]
    li.pop # 4
    print li # [1, 2, 3]
    
    li.remove(2) 
    print li # [1, 3]
    

    index

    Return the index of the specific element.
    li = [1, 2, 3, 4]
    print li.index(3) # 2
    

    reverse

    Reverse the list.
    li = [1, 2, 3, 4]
    li.reverse()
    print li # [4, 3, 2, 1]
    

    count

    Count how many times an element appears in the list.
    li = [1, 2, 3, 4, 1, 1, 2]
    print li.count(1) # 3
    
    mrdaniel wrote this tutorial on 1/3/16 | python, lists
    Comments
    Log in to submit a comment.