• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / Python / Using Python to Get and Print First N Items in List

Using Python to Get and Print First N Items in List

February 11, 2022 Leave a Comment

In Python, we can easily get and print the first n items of a list. To do so, we can use slicing and then use a loop to print each item. Below is a simple example showing how to print the first 10 items in a list.

list = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]

for x in list[:10]:
    print(x)

#Output:
0
1
2
3
4
5
6
7
8
9

When working with lists in our code, it can be useful to get the first few items from a collection.

We can easily use Python to get the first n elements in a list.

There are a few ways we can get the first n elements in a list to be able to print the items from a list in Python.

The easiest way to get the first n elements from a list is to use slicing in Python. Below shows the syntax of how to use slicing to get the first n elements of a list.

first_n_items = list[:n]

We can use this to print the first n elements of a list in Python.

Below is an example of how to print the first 5 elements from a list.

list = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]

for x in list[:5]:
    print(x)

#Output:
0
1
2
3
4

Printing the First 10 Items in List Using Python

Using Python, we can print the first 10 items of a list easily.

To do so, we just need to get the first 10 items, and then print them to the console.

We can print the first 10 items using a loop in Python as shown below.

list = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]

for x in list[:10]:
    print(x)

#Output:
0
1
2
3
4
5
6
7
8
9

You can also use the Python * operator to print out a list as well.

list = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]

print(*list[:10])
   
#Output:
0 1 2 3 4 5 6 7 8 9

How to Print the Last N Elements of a List

We can also print the last n elements of a list easily in Python.

In Python, we can easily get the last n elements of a list using slicing.

Below shows how we can use slicing in Python to get and print the last n elements of a list.

list_of_numbers = [1,0,4,2,-4,0,0,3,0,-1,0]

last_5_numbers = list_of_numbers[-5:]

print(*last_5_numbers)

#Output:
0 3 0 -1 0

Hopefully this article has been useful for you to understand how to print the first n items of a list in Python.

Other Articles You'll Also Like:

  • 1.  math.degrees() Python – How to Convert Radians to Degrees in Python
  • 2.  How to Iterate through a Set Using Python
  • 3.  pandas round – Round Numbers in Series or DataFrame
  • 4.  How to Group and Aggregate By Multiple Columns in Pandas
  • 5.  How to Remove Vowels from a String in Python
  • 6.  Using Python to Increment Dictionary Value
  • 7.  Python cos – Find Cosine of Number in Radians Using math.cos()
  • 8.  Using Python to Sort Two Lists Together
  • 9.  Clear File Contents with Python
  • 10.  Pascal’s Triangle in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy