How to Get First n Elements of a List in Python?
Hey Nerds,
In this blog i will explain python get first n Elements of list properties. Here you will get to learn how to get first n elements of a list in python. This example will help you python list get first N elements. Here is the solutions for get first N elements of list python. let see how we can achieve this using an example.
We will use [0:N] to get the first N elements of the python lists, in this example blog, 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 with the example:
Here i am using python 3 version:
Create a file with file name 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)
The output will be as follow
[1, 2, 3]
I hope this will help you get get first n Elements of list properties