Get First n Elements of List in Python

Get first n elements of lists in Python

Using List slicing

Use list slicing to get first n elements of List in Python. Here’s an example:

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.

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.

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.

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.

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.

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.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *