Table of Contents
Using List slicing
Use list slicing to get first n elements of List in Python. Here’s an example:
1 2 3 4 5 6 |
my_list = [10, 20, 30, 40, 50, 60] n = 4 first_n_elements = my_list[:n] print(first_n_elements) |
1 2 3 |
[10, 20, 30, 40] |
In the above example, list slicing is used to get a list’s first n
elements. As we can see, a list was defined as my_list
with 6
elements [10, 20, 30, 40, 50, 60]
. And a variable n
was defined with a value of 4
, representing the number of elements to be extracted from the list.
To obtain the first n
elements, list slicing was used by specifying the start index as 0
(which was the default) and the end index as n
. As a result, a new list was obtained, named first_n_elements
, which contained the first n
elements of my_list
.
Using List Comprehension
Use list comprehension to create a list with the first n
elements in Python.
1 2 3 4 5 6 |
original_list = ['India', 'China', 'Bhutan', 'Nepal', 'Thailand', 'France'] n = 3 new_list = [original_list[i] for i in range(n)] print(new_list) |
1 2 3 |
['India', 'China', 'Bhutan'] |
In this example, list comprehension creates a list with the first n
elements. An original list was defined as original_list
with six elements. And a variable n
was defined with a value of 3
, representing the number of elements extracted from the original list.
Then a list comprehension expression inside the square brackets [ ]
was used to create a new list containing elements from the original list with an index between 0
and n-1
. As a result, a new list was obtained and named new_list
, which contained the original list’s first 3
elements.
Further reading:
Using the itertools
Module
Use the itertools
module in python to get the list’s first n
elements by combining the islice
and iter
functions from the module.
1 2 3 4 5 6 7 |
import itertools original_list = ['Sun', 'Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'sat'] n = 5 first_n_elements = list(itertools.islice(iter(original_list ), n)) print(first_n_elements) |
1 2 3 |
['Sun', 'Mon', 'Tues', 'Wed', 'Thurs'] |
In the above code, the itertools
module is used. An original list was defined as original_list
with 7
elements representing weekdays, and variable n
was defined with a value of 5
, which meant the number of elements to be extracted from the original list. To get the list’s first n
elements, the islice
function from the itertools
module was used.
The islice
function took two arguments, as we can see islice(iter(original_list ), n)
:
- An iterator.
- The number of elements to return.
An iterator iter(original_list )
was created from the original list using the iter
function, and then the iterator and the value of n
were passed to the islice
function. As a result, a new list was obtained and named first_n_elements
, which contained the first 5
elements of the original list we wanted to extract.
if you want to get more information about the
itertools
module, check its official documentation in python.
Using for
Loop
Use the for
loop to get a list’s first n
elements in Python.
1 2 3 4 5 6 7 8 |
original_list = ['one', 'two', 'three', 'four', 'five', 'six', 'seven'] n = 2 first_n_elements = [] for i in range(n): first_n_elements.append(original_list[i]) print(first_n_elements) |
1 2 3 |
['one', 'two'] |
To get the list’s first n
elements, we used a for
loop in the above example that iterated over a range of numbers from 0
to n-1
. We appended the element at the corresponding index in the original list to the first_n_elements
list during each iteration. After the loop is completed, the first_n_elements
list contains the first 2
elements of the original list, which were ['one', 'two']
.
Using While
Loop
Use the While
loop to get the first n
elements of a list in Python.
1 2 3 4 5 6 7 8 9 10 |
my_list = [1, 2, 3, 4, 5, 6] n = 3 i = 0 new_list = [] while i < n: new_list.append(my_list[i]) i += 1 print(new_list) |
1 2 3 |
[1, 2, 3] |
The above code snippet used a while
loop to extract the specified number of elements from a list and store them in a new list. A list named my_list
is defined as containing the values [1, 2, 3, 4, 5, 6]
and a variable n
representing the number of elements we wanted to extract from my_list
.
Next, two variables, i
and new_list
, were initialized to 0
and an empty list []
, respectively. These variables were used to keep track of the current index of the element in the list and the extracted elements.
Then, a while
loop would continue as long as the value of i
was less than n
. Inside the loop, the current element of my_list
at index i
was extracted and appended to the new_list
. The value of i
was then incremented by 1 to move on to the next element. Once the while
loop was exited, the extracted elements in new_list
were printed out using the print()
function as [1, 2, 3]
.
That’s all about how to get first n elements of List in Python.