📱 Code Python on your phone with Python Coding - Learn PythonGet the free Android app →
  1. Home
  2. Python exercises
  3. List exercises

Python exercises · 6 tasks

Python list exercises

Lists are Python's workhorse. These six exercises practise building, searching, sorting and transforming them — finishing with a classic merge.

Quick answerPractise Python lists with six auto-checked tasks: total and average, second largest number, removing duplicates while keeping order, squaring even numbers with a list comprehension, rotating a list and merging two sorted lists. Each task reads numbers from input so you can test it like a real program.

0 / 6 solved

Reading a list from input

Each exercise gives you a line of numbers separated by spaces, like 4 8 15 16 23 42. Turn it into a list of integers with:

nums = [int(x) for x in input().split()]

Then use len(nums), sum(nums), sorted(nums), slicing and loops. Print a list directly with print(nums), or as space-separated values with print(*nums).

Exercises

1. Total, smallest, largesteasy

Read a line of numbers and print three lines: the total, the smallest and the largest.

    Show solution
    nums = [int(x) for x in input().split()]
    print(sum(nums))
    print(min(nums))
    print(max(nums))
    

    The built-ins loop over the list for you; writing the loops by hand is a good extra exercise.

    2. Second largestmedium

    Print the second largest distinct number. For 5 9 9 3 the answer is 5.

      Show solution
      nums = [int(x) for x in input().split()]
      print(sorted(set(nums))[-2])
      

      set drops the repeated 9 so the second-to-last item is genuinely the second largest value.

      3. Remove duplicates, keep ordermedium

      Print the numbers with duplicates removed, keeping the order of first appearance, separated by spaces. 3 1 3 2 1 → 3 1 2.

        Show solution
        nums = [int(x) for x in input().split()]
        seen = set()
        result = []
        for n in nums:
            if n not in seen:
                seen.add(n)
                result.append(n)
        print(*result)
        

        A set can't keep order, which is why we also build a list. Shortcut: list(dict.fromkeys(nums)).

        4. Squares of the even numberseasy

        Use a list comprehension to print a list of the squares of the even numbers only. 1 2 3 4 → [4, 16].

          Show solution
          nums = [int(x) for x in input().split()]
          print([n * n for n in nums if n % 2 == 0])
          

          A comprehension reads like English: n squared, for each n in nums, if n is even.

          5. Rotate a listmedium

          The first line has the numbers, the second line a number k. Rotate the list k places to the right and print it space-separated. 1 2 3 4 5 with k = 2 → 4 5 1 2 3. k may be bigger than the list.

            Show solution
            nums = [int(x) for x in input().split()]
            k = int(input()) % len(nums)
            print(*(nums[-k:] + nums[:-k] if k else nums))
            

            When k is 0, nums[-0:] is the whole list, so that case is handled separately.

            6. Merge two sorted listshard

            Two lines, each a sorted list. Print one sorted list containing all the numbers — without calling sort() or sorted().

              Show solution
              a = [int(x) for x in input().split()]
              b = [int(x) for x in input().split()]
              i = j = 0
              out = []
              while i < len(a) and j < len(b):
                  if a[i] <= b[j]:
                      out.append(a[i])
                      i += 1
                  else:
                      out.append(b[j])
                      j += 1
              out += a[i:] + b[j:]
              print(*out)
              

              This is the merge step of merge sort. It touches each number once, which is faster than sorting the combined list.

              Next topic: Loop exercises →

              Practise on your phone too

              Python Coding - Learn Python for Android: a real Python 3 interpreter, code editor, 26 example programs and 12 marked exercises.

              Get it on Google Play

              Frequently asked questions

              How do I read a list of numbers from input in Python?

              nums = [int(x) for x in input().split()] splits the line on spaces and converts each piece to an int.

              What is a list comprehension?

              A compact way to build a list: [expression for item in iterable if condition].

              How do I print a list without brackets?

              Unpack it into print: print(*nums) prints the items separated by spaces.

              Keep practising

              Updated 2026-10-22 · every solution is tested on Python 3