How to Get First N Items from a List in Python
Last Updated :
21 Jun, 2025
Accessing elements in a list has many types and variations. For example, consider a list "l = [1,2,3,4,5,6,7]" and N=3, for the given N, our required result is = [1,2,3].
This article discusses several ways to fetch the first N elements of a given list.
Method 1: Using List Slicing
This problem can be performed in 1 line rather than using a loop using the list-slicing functionality provided by Python.
Python
l = [1, 2, 3, 4, 5, 6, 6]
N = 2
res = l[:N]
print(str(res))
Explanation: l[:N] gives the first 'N' elements of the list.
Method 2: Using Loops
1. Using While Loop
In this approach, we will be using a while loop with a counter variable starting from 0 and as soon as its value becomes equal to the number of elements we would like to have we will break the loop.
Python
l = [1, 2, 3, 4, 5, 6, 6, 6, 6]
N = 4
i = 0
while True:
print(test_list[i])
i = i + 1
if i == N:
break
2. Using for Loop
This approach is more or less similar to the last one because the working of the code is approximately similar only benefit here is that we are not supposed to maintain a counter variable to check whether the required number of first elements has been printed or not.
Python
l = [1, 2, 3, 4, 5, 6, 6, 6, 6]
N = 3
for i in range(N):
print(l[i])
Method 3: Using List comprehension
Python List comprehension provides a much more short syntax for creating a new list based on the values of an existing list. It is faster, requires fewer lines of code, and transforms an iterative statement into a formula.
Python
l = [1, 2, 3, 4, 5, 6, 6, 6, 6]
N = 5
res = [idx for idx in l if idx < N + 1]
print(str(res))
Method 4: Using Slice Function
Python slice() function is used to get the first N terms in a list. This function returns a slice object which is used to specify how to slice a sequence. One argument is passed in this function which is basically the last index till which we want to print the list.
Python
l = [1, 2, 3, 4, 5, 6, 6, 6, 6]
N = 3
print(l[slice(N)])
The itertools module in Python provides several functions that allow us to work with iterators more efficiently. One such function is islice, which returns an iterator that produces selected items from the input iterator, or an iterable object, by skipping items and limiting the number of items produced.
Python
from itertools import islice
l = [1, 2, 3, 4, 5, 6, 6]
N = 3
res = islice(l, N)
print(list(res))
Similar Reads
How to Remove Item from a List in Python Lists in Python have various built-in methods to remove items such as remove, pop, del and clear methods. Removing elements from a list can be done in various ways depending on whether we want to remove based on the value of the element or index.The simplest way to remove an element from a list by i
3 min read
How to Find Length of a list in Python The length of a list means the number of elements it contains. In-Built len() function can be used to find the length of an object by passing the object within the parentheses. Here is the Python example to find the length of a list using len().Pythona1 = [10, 50, 30, 40] n = len(a1) print("Size of
2 min read
How to Initialize a List in Python Python List is an ordered collections on items, where we can insert, modify and delete the values. Let's first see how to initialize the list in Python with help of different examples. Initialize list using square brackets []Using [] we can initialize an empty list or list with some items. Python# I
2 min read
How toGet First and Last Elements from a Tuple in Python Tuples are immutable sequences in Python that can store a collection of items. Often, we might need to the retrieve the first and last elements from the tuple. In this article, we'll explore how to achieve this using the various methods in Python. Get First and Last Elements from a Tuple Using Index
2 min read
Remove first element from list in Python The task of removing the first element from a list in Python involves modifying the original list by either deleting, popping, or slicing the first element. Each method provides a different approach to achieving this. For example, given a list a = [1, 2, 3, 4], removing the first element results in
2 min read