Follow

Follow
Printing the First N Elements of a List in Python

Printing the First N Elements of a List in Python

Khushnood Asif's photo
Khushnood Asif
·Jan 8, 2023·

1 min read

Play this article

Have you ever needed to print the first few elements of a list in Python? It's a common task that can be easily accomplished using a combination of list indexing and a for loop.

First, let's define the list we want to work with.

L = [2,4,8,23,74,2,82,43,83,98,22,56,45]

To print the first N elements of this list, we use a for loop to iterate through the indices of the list and print the element at each index. Since list indices start at 0, we use the range() function to run the loop from 0 to N-1.

for i in range(N):
    print(L[i])

And that's it! With just a few lines of code, we can easily print the first N elements of a list in Python.

I hope this helps clarify the logic and approach for printing the first N elements of a list in Python.

 
Share this