How to Get First N Items from a List in Python
Last Updated :
08 Apr, 2025
Accessing elements in a list has many types and variations. This article discusses ways to fetch the first N elements of the list.
Using List Slicing to Get First N Items from a Python List
This problem can be performed in 1 line rather than using a loop using the list-slicing functionality provided by Python.
Python
test_list = [1, 2, 3, 4, 5, 6, 6]
# printing original list
print("The original list : " + str(test_list))
N = 2
# using list slicing
# Get first N elements from list
res = test_list[:N]
# print result
print("The first N elements of list are : " + str(res))
Output:
The original list : [1, 2, 3, 4, 5, 6, 6]
The first N elements of list are : [1, 2]
Time Complexity: O(N) where n is the required number of elements.
Auxiliary Space: O(N) Since we are storing our results in a separate list
Using Loops in Python
Here we will use the loop technique to traverse the first N item from the list in Python.
Using While Loop to Get First N Items from a Python List
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
test_list = [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
Output:
1
2
3
4
Time Complexity: O(N) where n is the required number of elements.
Auxiliary Space: O(1) Since we are not storing our results in a separate list
Using for Loop to Get First N Items from a Python List
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
test_list = [1, 2, 3, 4, 5, 6, 6, 6, 6]
N = 3
for i in range(N):
print(test_list[i])
Output:
1
2
3
Time Complexity: O(N) where n is the required number of elements.
Auxiliary Space: O(1) Since we are not storing our results in a separate list
Using List comprehension to Get First N Items from a Python List
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
# initializing list
test_list = [1, 2, 3, 4, 5, 6, 6, 6, 6]
N = 5
# printing original list
print("The original list : " + str(test_list))
res = [idx for idx in test_list if idx < N + 1]
# print result
print("Result : " + str(res))
Output:
The original list : [1, 2, 3, 4, 5, 6, 6, 6, 6]
Result : [1, 2, 3, 4, 5]
Time Complexity: O(N) where n is the required number of elements.
Auxiliary Space: O(N) Since we are storing our results in a separate list
Using Slice Function in Python to get the first N Items
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
# initializing list
test_list = [1, 2, 3, 4, 5, 6, 6, 6, 6]
N = 3
# printing original list
print("The original list : " + str(test_list))
# print result
print("Result : ",end="")
print(test_list[slice(N)])
OutputThe original list : [1, 2, 3, 4, 5, 6, 6, 6, 6]
Result : [1, 2, 3]
Time Complexity: O(N) where n is the required number of elements.
Auxiliary Space: O(1) Since we are not storing our results in a separate list
Using the itertools module:
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.
For example:
Python
from itertools import islice
test_list = [1, 2, 3, 4, 5, 6, 6]
N = 3
first_n_items = islice(test_list, N)
print(list(first_n_items)) # Output: [1, 2, 3]
Output:
[1, 2, 3]
Time Complexity: O(N) where n is the required number of elements.
Auxiliary Space: O(N) Since we are storing our results in a separate list
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
3 min read
Get a list as input from user in Python
We often encounter a situation when we need to take a number/string as input from the user. In this article, we will see how to take a list as input from the user using Python. Get list as input Using split() MethodThe input() function can be combined with split() to accept multiple elements in a si
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(). [GFGTABS] Python a1 = [10, 50, 30, 40] n = len(a1) pri
3 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. [GFGTABS]
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
How to read specific lines from a File in Python?
Text files are composed of plain text content. Text files are also known as flat files or plain files. Python provides easy support to read and access the content within the file. Text files are first opened and then the content is accessed from it in the order of lines. By default, the line numbers
3 min read
How to get the first and last elements of Deque in Python?
Deque is a Double Ended Queue which is implemented using the collections module in Python. Let us see how can we get the first and the last value in a Deque. Method 1: Accessing the elements by their index. The deque data structure from the collections module does not have a peek method, but similar
2 min read
Access List Items in Python
Accessing elements of a list is a common operation and can be done using different techniques. Below, we explore these methods in order of efficiency and their use cases. Indexing is the simplest and most direct way to access specific items in a list. Every item in a list has an index starting from
2 min read
How To Do Pagination In Python
In this article, we'll walk you through the steps of setting up pagination in Python. We'll explain each step clearly and straightforwardly. To help you understand the idea of pagination in Python, we'll show you how to create a pagination system using the Tkinter library. We'll start by explaining
5 min read