- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to get the first item from the array
In programming array is a data structure, which is used to store collection of homogeneous data elements. And each element in the array is identified by an index value or key.
Arrays in Python
Python doesn’t have a built-in data structure to represent arrays, but it has a built-in array module for arrays. And also we can use the NumPy package to work with arrays in python.
An array defined by the array module is −
array('i', [1, 2, 3, 4])
A Numpy array defined by the NumPy module is −
array([1, 2, 3, 4])
Also, we can use the list data structure to represent arrays, for that we need to store homogeneous items into the list.
Input Output Scenarios
Let’s see the below input output scenarios to understand how to get the first item from the array. Assume we have an array with 4 elements. And in output array the first element will be displayed.
Input array: [1, 2, 3, 4, 5] Output: [1]
Below examples we will use indexing and slicing features to access the first element from an array.
Python indexing is nothing but accessing the elements of a sequence by using their individual positional values, and it starts from 0 to n-1. In the same way, the slicing is used to access the group of elements from a sequence.
Syntax
Array[start : end]
Start − The starting index where slicing of an array starts.
End − The ending index where slicing of the array stops. This value is not inclusive.
Using List
The Python lists are zero-indexed, which means the first element is available at index 0.
Example
# creating array lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] print ("The original array is: ", lst) print() # get first element result = lst[0] print ("The first element is: ", result)
Output
The original array is: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] The first element is: 1
We accessed the first element by using list indenting lst[0]. Similarly, we can use the slicing to get the first elements. See the below example to get the first elements using slicing.
Example
# creating array lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] print ("The original array is: ", lst) print() # get first element result = lst[:1] print ("The first element is: ", result)
Output
The original array is: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] The first element is: [1]
The slicing syntax [:1] returns a first element then it is stored in the result variable.
Using Array Module
By using array() method we can converts the python list into an array, and this array also supports the indexing and array slicing.
Example
Let’s access the first item of an array using the index value.
import array # creating array array = array.array('i', [1, 3, 4, 8]) print ("The original array is: ", array) print() # get the first element result = array[0] print ("The first element is: ", result)
Output
The original array is: array('i', [1, 3, 4, 8]) The first element is: 1
Example
In this example, we will access the first item of an array using the array slicing.
import array # creating array array = array.array('i', [1, 3, 4, 8]) print ("The original array is: ", array) print() # get the first element result = array[:1] print ("The first element is: ", result)
Output
The original array is: array('i', [1, 3, 4, 8]) The first element is: 1
The statement array[:1] mean retrieving the array element upto the index value 1.
Using NumPy Array
Using the numpy array we can easily access the array elements.
Example
The index of a numpy array also starts from 0.
import numpy # creating array numpy_array = numpy.random.randint(1, 10, 4) print ("The original array is: ", numpy_array) print() # get the first element result = numpy_array[0] print ("The first element is: ", result)
Output
The original array is: [5 4 2 1] The first element is: 5
In the above example we have accessed the 1st element and the element is 5.
Example
We can use slicing syntax [:1] to get the first element. Let’s see in the example below.
import numpy # creating array numpy_array = numpy.random.randint(1, 10, 4) print ("The original array is: ", numpy_array) print() # get the first element result = numpy_array[:1] print ("The first element is: ", result)
Output
The original array is: [6 2 1 6] The first element is: [6]
The first element 6 is accessed from the numpy using slicing.