How to Get First n Elements of a List in Python?

By Hardik Savani October 30, 2023 Category : Python

Hey Folks,

This detailed guide will cover python get first n elements of list. I would like to share with you how to get first n elements of a list in python. This example will help you python list get first n elements. Here you will learn get first n elements of list python. So, let us see in detail an example.

We can use [0:N] to get the first n elements of the python list. In this example, we will take myList variable with numbers of list. Then we will add n variable to get the first n number of items in the python list. let's see the examples:

You can use these examples with python3 (Python 3) version.

Example:

main.py

# Create New List with Item
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
  
n = 3

# Python Get Get 2 Elements from List
newList = myList[0:n]
  
print(newList)

Output:

[1, 2, 3]

I hope it can help you...

Tags :
Shares