Skip to content
Laviyx Learn

Try

Python Lists Practice

Practise indexing, slicing, adding, removing and summarising list items.

Beginner3 problemsUpdated

Review first: Lists

  1. 1.First, last and middle

    Beginner

    Given days = ["Mon", "Tue", "Wed", "Thu", "Fri"], print the first day, the last day, and a list of the three middle days.

    Show hint

    Use index 0, index -1, and the slice [1:4] (or [1:-1]).

    Show solution
    Python
    days = ["Mon", "Tue", "Wed", "Thu", "Fri"]
    
    print(days[0])
    print(days[-1])
    print(days[1:-1])
    Output
    Mon
    Fri
    ['Tue', 'Wed', 'Thu']
  2. 2.Shopping cart

    Beginner

    Start with cart = ["milk", "bread"]. Add "eggs" to the end, insert "butter" at the start, remove "bread", and print the cart and its length.

    Show hint

    Use append(), insert(0, ...) and remove(), then len().

    Show solution
    Python
    cart = ["milk", "bread"]
    
    cart.append("eggs")
    cart.insert(0, "butter")
    cart.remove("bread")
    
    print(cart, len(cart))
    Output
    ['butter', 'milk', 'eggs'] 3
  3. 3.Top three scores

    Beginner

    Given scores = [67, 91, 78, 85, 99, 72], print the three highest scores from highest to lowest, without changing the original list.

    Show hint

    sorted() returns a new list. Sort in reverse and slice the first three.

    Show solution
    Python
    scores = [67, 91, 78, 85, 99, 72]
    
    top_three = sorted(scores, reverse=True)[:3]
    
    print(top_three)
    print(scores)
    Output
    [99, 91, 85]
    [67, 91, 78, 85, 99, 72]

    scores.sort(reverse=True) would also work for the ranking, but it reorders the original list, which the task said not to do.