Today in this tutorial we are going to know about arrays in python. This tutorial contains a brief explanation of arrays that will help you mastering this module. Also, we have shared some of the best examples you can try to learn arrays perfectly. Before getting into programming let's know what is an array?
In this post we will cover
What is an Array?
An array is a group or collection of items will memory locations. In short, an array is basically used to store multiple items of the same type. It is easier to calculate the position of elements by using an array. Just add the offset to a base value. Let's learn array with a short example.
Syntax:
variable name= ["value1", "value2", "value3"]
|
If we have a list of items of bike names, we have to store bikes' names in different variables.
Bike1 = "BMW"
Bike2= "Bullet"
Bike3= "Yamaha"
To avoid that we are going to use an array to store all values in the same variable. Here is how to do that.
Bike = ["BMW","Bullet","Yamaha"]
Summary
An array is just a variable which can hold multiple value at a time.
Let's explore more about an array.
How to access the elements of an array in Python
An array can hold many values in a single name and can be indexed using an index number. To access any element in the array we are going to use this syntax
x= Bike[0] |
The program to access elements in the above example will be
bikes = ["BMW","Bullet","Yamaha"]
x = bikes[0]
print(x)
Output:
BMW
Modify the value of an array item in Python
It is very easy to modify the value of an array item. To do this we just have to specify the indexed number which we have to change and the new value. Here is the syntax to modify the value.
variable_name[index number] = "New value"
Like in the above example we want to change the value of the 0th index item. To do that we will use the following code
bikes = ["BMW","Bullet","Yamaha"]
bikes[0] = "Hero"
print(bikes)
|
Output:
['Hero', 'Bullet', 'Yamaha']
This modification is usually used to change the value when the data of the length of an array is too big. Now here the question arises how to check the length of an array?
Length of an array in Python
To check the length of our array we will use the len() function of python. It simply helps us in checking the exact length of our array. To know more built-in functions in python, you can go through this article. Here is the syntax to do that.
variable = len(array_name)
|
Now let's check the length of the above example. To do this we will use the code
x = len(bikes)
Here is the full program
bikes = ["BMW","Bullet","Yamaha"]
x = len(bikes)
print(x)
|
Output:
3
How to add Elements to Array in Python
If we have to add elements in the array we will use the append() method. This method helps us in adding one more element to the array easily without changing the whole array. Have a look at the below program to add the elements in the array
bikes = ["BMW","Bullet","Yamaha"]
// Now use the append function to add the hero element in the array
bikes.append("Hero")
// print the updated array
print(bikes)
|
Output:
['BMW', 'Bullet', 'Yamaha', 'Hero']
Remove Elements from array in Python
We can also remove any elements from the array. To do that we will use the pop() method. This method will basically remove the given value from the array. Let's program to remove the array elements
bikes = ["BMW","Bullet","Yamaha"]
// Added hero in the array using append() method
bikes.append("Hero")
print(bikes)
// removed value indexed at 1 by using pop() method
bikes.pop(1)
print(bikes)
|
Output:
['BMW', 'Bullet', 'Yamaha', 'Hero']
['BMW', 'Yamaha', 'Hero'] //Bullet is removed as it is indexed in the 1th place
To remove an element from an array we can also use the remove() method. Here is the program to use the remove() method
cars = ["BMW", "LAMBO", "JAGUAR"]
// Used a remove function to remove the lambo from the array.
cars.remove("LAMBO")
print(cars)
|
Output:
['BMW', 'JAGUAR']
Now as we know the basics of an array. How to use, modify, add, remove and check the length of an array. Now we are going to learn about the use of an array in loop elements.
Iterate Items from Array in Python
To loop array elements we are going to use for loops. Here is a syntax to do this
for x in bikes: Here is the full program to use a loop in the above example.
bikes = ["BMW","Bullet","Yamaha"]
for x in bikes:
print(x)
|
Output:
BMW
Bullet
Yamaha
Array Properties in Python
Method |
Description |
append() |
To add elements in the array |
clear() |
To remove all the elements from the array |
copy() |
To return a copy of the given array |
count() |
It basically returns the number of elements with their value |
extend() |
To add an element at the end of an array |
index() |
This method returns the index of the first element with their value |
insert() |
Adds an element at the specified position |
pop() |
To remove the element from the given position |
remove() |
To remove the item from an array |
reverse() |
It reverses the array |
sort() |
It is used to sort the list |
Some More Examples to Practice Arrays
Example 1
a = [1.1, 3.5, 4.5] |
Output:
[1.1, 3.5, 4.5]
Example 2
#Python program to demonstrate to Remove elements in a Array
import array
# To initializing array with some values
arr = array.array('i', [10, 20, 30, 100, 15])
# printing original array
print ("The new created array is: ")
for i in range (0, 5):
print (arr[i])
print ("\r")
# Now using pop() method to remove element from 1rst position
print ("The popped element is: ")
print (arr.pop(1))
# To print the array after popping
print ("The array after popping is: ")
for i in range (0, 4):
print (arr[i])
print("\r")
# To print array after removing the value
print ("The array after removing is: ")
for i in range (0, 3):
print (arr[i])
|
Output:
The new created array is: 10 20 30 100 15
The popped element is: 20
The array after popping is: 10 30 100 15
The array after removing is: 10 30 100
Example 3
# Python code to search an element in the array
# importing the array module in python
import array
# To initialize array with array values
arr = array.array('i', [10, 20, 30, 21, 22, 35])
# To print an original array
print ("The new created array is: ")
for i in range (0, 6):
print (arr[i] )
print ("\r")
# Now using index() method to print the search of 20
print ("The indexex position of 20 is: ")
print (arr.index(20))
# Again using index() method to print index the position 21
print ("The indexed position of 21 is: ")
print (arr.index(21))
|
Output:
The new created array is: 10 20 30 21 22 35
The index of 1st occurrence of 2 is: 1
The index of 1st occurrence of 1 is: 3
Keep practicing more programs to master the array in python programming. Here we have explained everything in brief about the arrays in python. Also, do not forget to try other built-in methods in your programming. If you face any problem, please let us know in comments we will surely help you out
Related Topics
Python Program to convert List To String
Index error : String index out of range error
15 python Project Ideas for begginers
Article Contributed By :
|
|
|
|
1034 Views |