Start learning free

LESSON 119 OF 673 · LISTS - CHALLENGES

First N Items in Python: Slicing a List

Practice Read the full explanation

You get a list of integers nums and a number n. Return a list with the first n numbers of nums.

Keep them in the same order. When n is 0, return an empty list.

 

Example 1:

Input: nums = [3, 8, 1, 9, 4], n = 3
Output: [3, 8, 1]
Explanation: The first 3 numbers are 3, 8 and 1.
The 9 and the 4 are left out.

Example 2:

Input: nums = [5, 6, 7], n = 0
Output: []
Explanation: No numbers are taken, so the result is empty.

Example 3:

Input: nums = [2, 4], n = 2
Output: [2, 4]
Explanation: n is the length of nums, so every number is kept.

 

Constraints:

  • nums has 1 to 10 numbers.
  • Every number is an integer from -1000 to 1000.
  • n is from 0 to the length of nums.
STEP 1 OF 2
    Ctrl + Enter to check

    PYTHON · LISTS - CHALLENGES

    First N Items in Python, explained

    Problem overview

    The method firstN gets a list of integers nums and a number n, and returns a list with the first n numbers, in the same order. When n is 0 the result is an empty list. The constraints keep n between 0 and the length of nums, so you never have to ask for more items than exist.

    Three examples show the range of cases:

    • nums = [3, 8, 1, 9, 4] and n = 3 give [3, 8, 1]. The 9 and the 4 are left out.
    • nums = [5, 6, 7] and n = 0 give [].
    • nums = [2, 4] and n = 2 give [2, 4], because n equals the length and every number is kept.

    Approach: one slice

    A loop that appends items one by one would work, but Python has a tool made for this job: the slice. nums[:n] is a new list that starts at index 0 and stops just before index n. Its items sit at indexes 0 to n - 1, which is exactly n items. The end index is never included, so no - 1 is needed.

    The edge cases need no special code. When n is 0 the slice nums[:0] has nothing between the start and the end, so it is an empty list. When n equals len(nums) the slice covers the whole list and gives a copy of it.

    Step by step

    1. Slice nums from the start up to index n and store the result in result. With nothing before the colon, the slice starts at index 0.
    2. Return result. Slicing builds a new list, so nums itself is left as it was.

    The solution in Python

    class Solution:
        def firstN(self, nums: List[int], n: int) -> List[int]:
            result = nums[:n]
            return result
    

    Run on the three examples, it prints:

    [3, 8, 1]
    []
    [2, 4]
    

    The annotations List[int] and -> List[int] only describe the types for the reader. Python does not check them when the code runs. The self parameter belongs to the class and you don't pass it yourself.

    Another way: a loop

    The same result comes from building the list by hand. It is longer, but it shows what the slice does for you:

    def first_n_loop(nums, n):
        result = []
        for i in range(n):
            result.append(nums[i])
        return result
    

    range(n) gives the indexes 0 to n - 1, and each pass copies one number. With n = 0 the loop body never runs and the empty result is returned. On the three examples it gives the same lists as the slice: [3, 8, 1], [] and [2, 4]. Because the constraints keep n within the length, nums[i] never goes out of range here. A slice would also forgive an n that was too big, while the loop would raise an IndexError.

    Edge cases and common mistakes

    • Writing nums[:n - 1]. That drops one number too many. For the first example it gives [3, 8].
    • Writing nums[n]. One index gives a single number, not a list. With n = 3 it returns 9, and with n equal to the length it raises an IndexError.
    • Using a negative end. nums[:-1] means "all but the last", counting from the end. The slice nums[:n] counts from the start, which is what the task asks for.
    • Worrying about n = 0. No if is needed. nums[:0] is already [].

    Testing

    Try the three examples above, and also a list of one number with n = 1 (the result is that same number in a list). If the output is right for n = 0 and for n = len(nums), the ends of the range are right.

    Keep going with Python500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free