• 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
  • Ruby
  • 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.  Using Python to Check if String Contains Only Letters
  • 2.  Print Multiple Variables in Python
  • 3.  How to Write Python Dictionary to File
  • 4.  math.radians() python – How to Convert Degrees to Radians in Python
  • 5.  Python acosh – Find Hyperbolic Arccosine of Number Using math.acosh()
  • 6.  Changing Python Turtle Size with turtlesize() Function
  • 7.  Using Python to Find Maximum Value in List
  • 8.  Python tostring method – Convert an Object to String with str() Function
  • 9.  Python Prepend to List with insert() Function
  • 10.  Count Values by Key in Python Dictionary

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

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

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 © 2023 · The Programming Expert · About · Privacy Policy