How To Get Elements From Array In Python

We Can Create Array Of Same Data Types In Python Using List. In This Post We Will See How To Extract Values From List Of Array In Python


Extract Elements From Array In Python

array1=[5,7,2,9,10,6,1,2,9,3]

To Extract Elements From This Array We Can Use Substring Syntax By Passing Index's Position


Printing First Element Of Array In Python

As We Know That The First Index Position Is 0 , So We Will Pass 0 In Substring Syntax

Code:
array1=[5,7,2,9,10,6,1,2,9,3]
print(array1[0])
Output
5

Print Last Element Of Array In Python

We Can Extract The Last Element From List In Python By Passing -1 In Substring

Code
array1=[5,7,2,9,10,6,1,2,9,3]
print(array1[-1])
Output
3

Extract First Five Element From Array In Python

Suppose There Is An Array Containing 10 Elements. Now We Want To Extract First Five Elements From That Array. For This We Can Perform Slicing Operation

Syntax
newarray=originalarray[:N]
Where N Is The Number

Code To Print First Five Element From List In Python

array1=[5,7,2,9,10,6,1,2,9,3]
newarray=array1[:5]
print(newarray)
Output
[5, 7, 2, 9, 10]

Extract Last Five Element From Array

Again Take The Previous Example , An Array Having 10 Elements. Now To Print Last 5 Elements From Array , We Will Perform Slicing Operation By Passing -5:

Code
array1=[5,7,2,9,10,6,1,2,9,3]
newarray=array1[-5:]
print(newarray)
Output
[6, 1, 2, 9, 3]