3

Get First and Last Elements of a List in Python Program

 1 year ago
source link: https://www.geeksforgeeks.org/videos/get-first-and-last-elements-of-a-list-in-python-program/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Get First and Last Elements of a List in Python Program

Get First and Last Elements of a List in Python Program
Welcome to keys for geeks today, we're going to learn
  • 300 Views
  • 16/08/2022

In this video, we will learn write a python program to get the first and last elements of a list. 

Below is the list of approaches that we will cover: 
1. Getting the elements using list index. 
2. Getting the element using list slicing 

To get the element we are using list indexing in which we can pass the 0 index number and length of list -1 to get the last element of the list, we can also use the negative indexing to get the element from the list. This is most naïve method to achieve this particular task one can think of. 

The another method is using list slicing, It technique to perform the particular task of getting first and last element. We can use step of whole list to skip to the last element after the first element.

Python program to get the first and last elements of a list:
https://www.geeksforgeeks.org/python-get-first-and-last-elements-of-a-list/


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK