Array In Python

Introduction

 
In this article, I will explain Array in Python. Arrays are used to store one or more values in one single variable. An array is a set of items saved at contiguous reminiscence locations. The concept is to store a couple of items of the identical kind together. This makes it less difficult to calculate the position of each element.
 
Array In Python
 

Access the Elements of an Array.

 
Array element is referring to the index number.
 
Example
  1. color= ["red""blue""green"]  
  2. print (color [0]) #red is 0 position  
  3. print (color [2]) #green is 2 position  
Output
 
First array element and last array element value.
 
Array In Python 
 

Modifying elements

 
Change the array element. 
 
Example
  1. color= ["red""blue""green"]  
  2. color [0] ="black"  
  3. print(color)# red changes to black.  
Output
 
First array element value change.
 
Array In Python
 

Access the array Elements in reverse order.

 
The array element is accessed in a reverse direction.
 
Example
  1. color= ["red""blue""green"]  
  2. print (color [-1]) #green is the last position.  
Output
 
Last array element value.
 
Array In Python
 

The length of an array.

 
The len() method is used to return the number of elements present in an array.
 
Syntax
  1. Len(list)  
Example
  1. color= ["red""blue""green"]  
  2. print(len(color))  
Output
 
Return the number of elements in the “color” array.
 
Array In Python
 

Adding array elements.

 
The append () method is used to add an element in the last position of the array.
 
Syntax
  1. list.append()  
Example
  1. color= ["red""blue""green"]  
  2. color.append("yellow")  
  3. print(color)#add the element in the last position.  
Output
 
Add an element in the last position.
 
Array In Python
 

Removing array elements.

 
The pop () method is used to remove an element from the array.
 
Syntax
  1. list.pop()  
Example
  1. color= ["red""blue""green"]  
  2. color.pop(1)  
  3. print(color)#remove array 1.  
Output
 
Remove the element from the array.
 
Array In Python
 

Reverse array element.

 
The reverse () method is used to reverse the order of elements.
 
Syntax
  1. list.reverse()  
Example
  1. color= ["red""blue""green"]  
  2. color.reverse()  
  3. print(color)#reverse the elements.  
Output
 
Reverse the order of elements
 
Array In Python
 

Count array element.

 
The count () method is used to return the number of elements with the specified value.
 
Syntax
  1. list.count()  
Example
  1. color= ["red""blue""green"]  
  2. print(color.count("blue"))#blue color is which position.  
Output
 
Array In Python
 

Extend array element.

 
The extend () method is used to add two list elements.
 
Syntax
  1. extend.append()  
Example
  1. color= ["red""blue""green"]#first list  
  2. animal=["rat","cat","dog"]#second list  
  3. color.extend(animal)  
  4. print(color)#add two list  
Output
 
Add two list elements.
 
Array In Python
 

Conclusion

 
In this article, we have seen the Array in Python. I hope this article was useful to you. Thanks for reading!


Similar Articles