ItSolutionStuff.com

How to Get Last 2, 3, 4, 5, 10, etc Elements from List in Python?

By Hardik Savani โ€ข October 30, 2023
Python

Hi Friends,

In this tutorial, you will discover python get last 10 elements from list. Iรขโ‚ฌโ„ขm going to show you about python get last 4 elements from list. This example will help you how to get last 2 elements from list in python. you can understand a concept of how to get last 3 element from list in python. follow the below example for python list get last 5 elements.

I will give you 5 examples of how to get last two, three, four, five, ten elements from list. you can view one by one example that way you can use it what you need. we will use [-N:] to get last elements. let's see the examples:

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

Example: Python Get Last 2 Elements from List

main.py

# Create New List with Item
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
  
# Python Get Last 2 Elements from List
newList = myList[-2:]
  
print(newList)

Output:

[11, 12]

Example: Python Get Last 3 Elements from List

main.py

# Create New List with Item
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
  
# Python Get Last 3 Elements from List
newList = myList[-3:]
  
print(newList)

Output:

[10, 11, 12]

Example: Python Get Last 4 Elements from List

main.py

# Create New List with Item
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
  
# Python Get Last 4 Elements from List
newList = myList[-4:]
  
print(newList)

Output:

[9, 10, 11, 12]

Example: Python Get Last 5 Elements from List

main.py

# Create New List with Item
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
  
# Python Get Last 5 Elements from List
newList = myList[-5:]
  
print(newList)

Output:

[8, 9, 10, 11, 12]

Example: Python Get Last 10 Elements from List

main.py

# Create New List with Item
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
  
# Python Get Last 10 Elements from List
newList = myList[-10:]
  
print(newList)

Output:

[3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

I hope it can help you...

Tags: Python
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

๐Ÿ“บ Subscribe on YouTube

We Are Recommending You

โ˜…

Python Remove Last 2, 3, 4, 5, 10, etc Elements from List Example

Read Now โ†’
โ˜…

How to Remove First n Elements from List in Python?

Read Now โ†’
โ˜…

How to Remove Last n Elements from List in Python?

Read Now โ†’
โ˜…

Python List Replace Single with Double Quotes Example

Read Now โ†’
โ˜…

Python List Replace Double Quotes with Single Example

Read Now โ†’
โ˜…

Python Pretty Print List of Dictionaries Example

Read Now โ†’
โ˜…

Python Create JSON File from List Example

Read Now โ†’
โ˜…

How to Add Element at Specific Index in Python List?

Read Now โ†’
โ˜…

How to Find Sum of All Elements in List in Python?

Read Now โ†’
โ˜…

How to Find Average of List in Python?

Read Now โ†’
โ˜…

How to Convert List into String in Python?

Read Now โ†’
โ˜…

Python Remove Empty String from List Example

Read Now โ†’