How do I print the first and last elements of a list?

Python | Get first and last elements of a list

Sometimes, there might be a need to get the range between which a number lies in the list, for such applications we require to get the first and last element of the list. Let’s discuss certain ways to get the first and last element of the list.

Method #1 : Using list index
Using the list indices inside the master list can perform this particular task. This is most naive method to achieve this particular task one can think of.




# Python3 code to demonstrate

# to get first and last element of list

# using list indexing

# initializing list

test_list = [1, 5, 6, 7, 4]

# printing original list

print ["The original list is : " + str[test_list]]

# using list indexing

# to get first and last element of list

res = [ test_list[0], test_list[-1] ]

# printing result

print ["The first and last element of list are : " + str[res]]

Output: The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]


Method #2 : Using List slicing
One can also make use of list slicing 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.




# Python3 code to demonstrate

# to get first and last element of list

# using List slicing

# initializing list

test_list = [1, 5, 6, 7, 4]

# printing original list

print ["The original list is : " + str[test_list]]

# using List slicing

# to get first and last element of list

res = test_list[::len[test_list]-1]

# printing result

print ["The first and last element of list are : " + str[res]]

Output: The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]


Method #3 : Using list comprehension
List comprehension can be employed to provide a shorthand to the loop technique to find first and last element of the list. Naive method of finding is converted to a single line using this method.




# Python3 code to demonstrate

# to get first and last element of list

# using list comprehension

# initializing list

test_list = [1, 5, 6, 7, 4]

# printing original list

print ["The original list is : " + str[test_list]]

# using list comprehension

# to get first and last element of list

res = [ test_list[i] for i in [0, -1] ]

# printing result

print ["The first and last element of list are : " + str[res]]

Output: The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]




Article Tags :

Python

Python Programs

Python list-programs

Read Full Article

Get first and last elements of a list in Python

PythonServer Side ProgrammingProgramming

There may be situation when you need to get the first and last element of the list. The tricky part here is you have to keep track of the length of the list while finding out these elements from the lists. Below are the approaches which we can use to achieve this. But of course all the approaches involve using the index of the elements in the list.

Reading the First and Last Elements of a Python List

Elements in a list are numbered zero onwards. In this tutorial, we will learn different ways to access the first and the last elements from a list.

Let’s start by initializing a list.

1. Initialize a Python list

To initialize a list in Python use:

a = [6, 5, 9, 7]

This will initialize a new list ‘a‘ with the four elements we have mentioned.

2. Accessing list elements

You can access elements from the list by using the name of the list along with an index number. To print the first element use:

print[a[0]]

To print the last element use :

print[a[-1]]

Using -1 as the index gives us the last element from the list.

Python Code to Get First and Last Element of a List

The complete code is as follows :

a = [6, 5, 9, 7] print["first element is" ,a[0]] print["last element is", a[-1]]

Output

“python print first and last element of list” Code Answer


Python get first element from list

python by Hilarious Hoopoe on Sep 12 2020 Comment

2

Add a Grepper Answer


Python answers related to “python print first and last element of list”

  • python how to get the last element in a list
  • last element of list python
  • how to get the last value in a list python
  • how to find the last item of a list
  • how to print last element in a list python
  • get every item but the last item of python list
  • method get first last name python
  • how to get last element of list in python
  • python last element of list
  • python list last element
  • find last element in list python
  • python last item in list
  • find last index of element in list python
  • get the last element from the list
  • python get the last element from the list

Python queries related to “python print first and last element of list”

  • python get first element of list
  • first element of list python
  • get first item in list python
  • get first element of array python
  • get first value of list in python
  • python get first 5 elements of list
  • print function first last item list python
  • get value of first element in list python
  • how to get the first element of a list in python
  • python how to look at an the first item in the list
  • how to print the first thing in a list python
  • get list of list where first element is
  • pick first value in an array python
  • get the first item of python list
  • python get all first elements of list
  • ffirst element in list python
  • first item of list python
  • get first list from list of lists python
  • python get first element of set
  • take first element of array in python
  • python get first 3 elements in list
  • how to have the first thing in a list number 1 python
  • python check first element
  • get the first element of a list python
  • get first item of an array python
  • python sequence get first element
  • python first list
  • printing the first object in a list
  • select first element of list python
  • get first item from a list in python
  • find the first two elements of the list
  • how to return the first item of a list python
  • print first element of list python
  • get first item in array python
  • python print first and last element of list
  • get first item of list of lists python
  • get first items of list
  • get first value in a list python
  • get first of list python
  • python get dict first from list
  • first item from list python
  • take the first element of a list of lists
  • return the first element of an array python
  • get only first from list
  • python how to extract the first item from a list
  • how to get first index of list python\
  • get first row of list python
  • how to get first value of a list in python
  • how do i discover the first item in a list python
  • get all the first elments in list of list python
  • python get the element of the first list
  • how to get the first item of an list
  • python get first elements of list
  • python get first 3 items of list
  • extract first element of list and put into array python
  • how to get a list without the first element python
  • python list find first
  • python find first element of a list
  • how to get 1st element of list in python
  • ptyhon list take first
  • how to access first object in list python
  • how to get first value in a list python
  • select first element in array python
  • get first item in set python
  • python list extract first element
  • list extract first value python
  • python3 how to extract first item in a set
  • python lose first elemen of list
  • how to access first items in python list
  • python access first element in set
  • python get first x elements of list
  • how to show the first element of the list
  • find first python in list
  • what is the first element of a list
  • python select first element from list
  • make element first in the list python
  • how to get first item of list
  • print first item in array python
  • how to find first element of list in python
  • python select from list of list get 1st element from each
  • access list 1st item in array python
  • how to get first element of each array in list python
  • list of list first entry python
  • python return first n elements of list
  • how get first 3 of list
  • grab the first element of a list in value python
  • how to only move the first item of a list
  • hopython how to select first item in a list
  • python take first element
  • take out first element from list
  • get first list index
  • get the first element of an array python
  • python fisrt item
  • python how to print only the first item in a list
  • hpython how to print first and last item of a list
  • how to get first eleement of a list in python
  • how to choose the first item in a list python
  • how to make the first index the last index in a list python
  • get first item in a list python
  • python only print first item in for
  • how to print first element in list
  • how to get first elements in an array in python
  • first item on list
  • take first item of list python
  • how to print first items in a list python
  • python check first element of list
  • python get first element in array
  • python get first and last number from a list
  • python get first first item of list
  • get the first item of a list python
  • how to get the first value of a list in python
  • get first item from list python
  • first element python
  • for i in list python index 1st element
  • how to get the first element in a list python
  • return list from last to first
  • how to get list of lists first value
  • get first object in array python
  • how to get first item in list python
  • how to make a list of first index of a list of lists
  • get the first value out of a list python
  • how to print the first index of a list
  • how to get the first item of a list python
  • python get first 5 items in list
  • print first list element python
  • select first elemet in python
  • python print first x element in list
  • python sequence get first
  • python list get the first 10 element
  • get first element in a list python
  • the print first element of the list
  • get first elelment from list
  • liste first elements from a list pyhton
  • get first value of a python lit
  • python keep only first list element
  • get the first element of dictionary python
  • how to print the first item of a list of lists in python
  • first elt list python
  • python3 get first element of list
  • python get 1st item in list
  • how to get the first item in a list python
  • how to reference first item list python
  • python get first item in lsit
  • how to print list in first last pattern
  • print only the first item in a list python
  • python get first to
  • python get first element of every list in list of lists
  • how to access the first item of every list in the list in python
  • get first element of list python
  • python get first item in list
  • get first element python
  • how to print first element of list in python
  • python first[] last[]
  • select first elements in list python
  • get first value of list python
  • first and last two elemts of a list python
  • how to know with item comes first in python list
  • how to get the first matching element of a list in python
  • python list 1 list get first element
  • print first last item list python function
  • how to print first and last item in list python
  • how to get first and last element of list in python
  • get first index of value list python
  • select first item in list python
  • how to return the first element in an array in python
  • how access first and second item in list python
  • access first element of list python
  • python array get first element
  • how to get first element of list in python
  • get first value in list
  • print first element array python
  • get first element of dictionary python
  • python variable print 1st item in list
  • first item as a list is the same python
  • output first element from array python
  • python get the first element of a list
  • print first elements form list python
  • python first in list
  • python print first item in list
  • python get first element of array
  • define first value item in list python
  • first item in list
  • get first element list python
  • get first from list
  • get first elements of list python
  • get first element of a list
  • accessing first element in python list
  • create python function to get first number of list
  • get all first item in list python
  • python select first value from list
  • how to take out the first value of a list
  • python list read list from first item
  • python list of list access first element
  • from first element of array python
  • get first 100 of list
  • find first value in list python
  • how to access the first element in a list in python
  • python how to get first element in list
  • print first some element of list python
  • python get first 100 items in list
  • get the first 10 elemnts of a list python
  • python list get first element
  • how we can get first value from list in python new syntax
  • python extract first element from list
  • how to get first 5 element from list python
  • how we can get first value from list in python
  • list get first element
  • python safe get first element of list
  • python get x first from list
  • get first element from list
  • get the first value in list
  • dict with lists gives only first element of list
  • how to get first argument in list python
  • python list get first element or none
  • get first item in a set python
  • python choose every first element in list
  • get the first item in list from left
  • send first element of list to end python
  • python get first element of a list
  • python set get first element
  • another method to get first element of list in python
  • how to read first element of list in python
  • extract first element grom a list in purrr
  • get first 10 list in python
  • first item in items python
  • how to get the first array in a list python
  • how do you refer to the first value in your list in python
  • python first of list
  • find first instance in list python
  • how to print only first item in list
  • how to get elements after first elements in the list
  • python get first from all the lists
  • take first 5 elements of list python
  • only returning first letter of list pythonm
  • command to extract first elemnt in list python
  • python list access first element in each element
  • python get first element of list to string
  • extract the first string in list python
  • how to get first element of a list of list python
  • python get first item of array
  • python get the first item in a list
  • python list get first element object
  • how to get the first element of list in python
  • python how to print first element in list
  • get the first value of a list py
  • how to get first element from list python
  • get first element of a list python
  • reference first element of a list in python
  • get first values of list python
  • first item on list of list
  • grab first 6000 items list python
  • first of an element in a list pyton
  • print first last item list python
  • python3 get the first entry of a list
  • python get first item in array
  • select first element in a list
  • how to get first element of an array python
  • take first and last of a list pythin
  • python function print frst item in a set of lists
  • python list get the first element
  • python get fisrt item from list
  • python print out first item in list
  • python get first list entry
  • python how to take only first number of items in a list
  • python get first value in list
  • return the first one in python
  • for print first item last
  • extract the elements from the list python
  • get first entry of list python
  • first element of the list python
  • how to read first element of list inside list python
  • python first elemnt of list
  • access every first and second items in list python
  • function for giving the first element of list
  • write a function that returns the first element of a list python
  • take first 100 from list
  • python string list get first element
  • get first element of list of list python
  • take first words of list values python
  • get a list with the first element of a list
  • get first element in sub list python
  • python first item in list
  • get the frist enad las value in an list
  • python list first
  • python get all first elements of lits
  • first element in list comprenhension
  • python get first element in list
  • python get frist item from list
  • get first and last element of list python
  • how to get first utem in python
  • first element in a list python
  • get first item of a list python
  • first and last value show in for with flask
  • how to return the first item in a list python
  • python best way get first item in list
  • how to add first element from list python
  • python first element in list
  • python first element of list
  • python list first element
  • how to get the first item of a list in python
  • python return first item in list
  • how to get first element of array in python
  • get first element in list python
  • python get first item from list
  • print the first element of a list in python
  • first first value of an array in python
  • python get first elemtn in list
  • python get first element
  • how to assign first element of a sublist to a variable python
  • get from python list first certain elements
  • get first index of list python
  • how to check if the element is the 1st element iin list python
  • get first item list loop python
  • python set get first
  • get first value from array python
  • first item in list python
  • first value in list python
  • print first elemen found
  • first element list python
  • list get first element python
  • python get first in list
  • printing first some elements of list
  • python get first element from list
  • get first item of list python
  • check first element of list python
  • python get first
  • get first element from output in python
  • get the first value of a list in python
  • get first element array python
  • python list print first element
  • how to print the first element of a list in python
  • find first element in list python
  • python get first item from array
  • python get returning first element of list
  • get only first value from list python
  • get first value of list
  • python get first list item
  • python list select first value
  • list python get first element
  • get first items of list of lists python
  • python find first item in list
  • python print list first
  • how to access first item in list python
  • get first element function python
  • how to get the first element in list
  • python array extract first element
  • get the first item in a list python
  • get first from list of lists
  • get list without first element python
  • how to get the first part of a list in python
  • access the first list python
  • python get first 20 element of list
  • python get the first element of an array
  • how to find the first of a item in a list in python
  • python array only getting first element
  • get the first elemr in of the list
  • python access first element in list
  • how to get first element of a list in python
  • python list get first 10 elements in list
  • get first 10 items in list python
  • get first element of list of strings python
  • python print first element in list
  • python get first of list
  • relocate elements in a list to the first element python
  • get the first item in a list in python 3
  • list keep first element of each element
  • how to get list first element in python
  • get first element from set python
  • python read first item from list
  • python get list without first element
  • first element of list in python
  • only keep the first index in a list python
  • python3 get first 10 items from list
  • python list get first row
  • python get the first list#
  • first item in a list python
  • python get first element of list in list
  • first list python
  • python list find first element
  • get first value of array python
  • get elements from list after the first
  • python list first item
  • python list except first element
  • get the first 100 out of a list
  • how to select first objects in list in python
  • return first element in python list
  • find first item of list
  • how to call first element in list python
  • retrieve first value from list python
  • how to print first 5 items from the list text analysis python
  • get first and last element of a list
  • python select first item in a list
  • how get firet of the list python
  • take first element of a list python
  • how to select the first item in a list python
  • python found first elem of lsit
  • grab first element of list python
  • how to acces teh fiest item in a list from the the last index plus 1
  • python first item in list that
  • get first item of array python
  • how to get first index of a list
  • python list first and last element
  • python list get first and last element
  • extract any one element from list python
  • get first index of list in python
  • take the first from each list python
  • python first item of list
  • get first item of list in python
  • retain first 3 elements of list python
  • get first value from list python
  • python print first thing in list
  • python get first method
  • python get 1st element in list
  • first list element python
  • get first and last element list python
  • how to get the first element of python array
  • python get first element of list when list end riched
  • print first and last 10 list element spython
  • python3 get first item from list
  • pthon firs item in array
  • python list print first element of list
  • get the first and last value of a list python
  • get first item in python list
  • python find first in list
  • get first item of list
  • get first elment in list python
  • how to access the first item out of a list in python
  • python list get firstelement
  • print first items in the list in python
  • get 0th item of list python
  • accessing the first element of a list of lists
  • get first element pyspark
  • list.first in python
  • python first element in a list
  • how to get the first number in a list
  • python get first number of elements from list
  • get first item array python
  • python pick first item in list
  • python3 get first item in array
  • python list first last element
  • list get first python
  • python first element list
  • selecting all the first elements from a list of list
  • how to call the first item in a list of lists

Python: Generate and print a list of first and last 5 elements where the values are square of numbers between two numbers

Last update on October 08 2020 09:22:21 [UTC/GMT +8 hours]

Video liên quan

Bài mới nhất

Chủ Đề