Open In App

How to Get First N Items from a List in Python

Last Updated : 17 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Accessing elements in a list has many types and variations. These are essential parts of Python programming and one must have the knowledge to perform the same. This article discusses ways to fetch the first N elements of the list. Let’s discuss certain solutions to perform this task.

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

Python3




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.

Python3




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.

Python3




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.

Python3




# 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.

Python3




# 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)])


Output

The 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:

Python3




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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads