Array in Python | Set 1 (Introduction and Functions) - GeeksforGeeks

22 Jan, 2022
Other than some generic containers like list, Python in its definition can also handle containers with specified data types. The array can be handled in python by a module named “array“. They can be useful when we have to manipulate only specific data type values.
Operations on Array :
1. array(data type, value list):- This function is used to create an array with data type and value list specified in its arguments. Some data types are mentioned in the table below.
2. append():- This function is used to add the value mentioned in its arguments at the end of the array.3. insert(i,x) :- This function is used to add the value(x) at the ith position specified in its argument.
Python3
# Python code to demonstrate the working of# array(), append(), insert()  # importing "array" for array operationsimport array  # initializing array with array values# initializes array with signed integersarr = array.array('i', [1, 2, 3]) # printing original arrayprint ("The new created array is : ",end=" ")for i in range (0, 3):    print (arr[i], end=" ") print("\r") # using append() to insert new value at endarr.append(4); # printing appended arrayprint("The appended array is : ", end="")for i in range (0, 4):    print (arr[i], end=" ")     # using insert() to insert value at specific position# inserts 5 at 2nd positionarr.insert(2, 5) print("\r") # printing array after insertionprint ("The array after insertion is : ", end="")for i in range (0, 5):    print (arr[i], end=" ")
Output: 
The new created array is : 1 2 3 
The appended array is : 1 2 3 4 
The array after insertion is : 1 2 5 3 4 
4. pop():- This function removes the element at the position mentioned in its argument and returns it.5. remove():- This function is used to remove the first occurrence of the value mentioned in its arguments.
Python3
# Python code to demonstrate the working of# pop() and remove() # importing "array" for array operationsimport array # initializing array with array values# initializes array with signed integersarr= array.array('i',[1, 2, 3, 1, 5]) # printing original arrayprint ("The new created array is : ",end="")for i in range (0,5):    print (arr[i],end=" ") print ("\r") # using pop() to remove element at 2nd positionprint ("The popped element is : ",end="")print (arr.pop(2)); # printing array after poppingprint ("The array after popping is : ",end="")for i in range (0,4):    print (arr[i],end=" ") print("\r") # using remove() to remove 1st occurrence of 1arr.remove(1) # printing array after removingprint ("The array after removing is : ",end="")for i in range (0,3):    print (arr[i],end=" ")
Output: 
The new created array is : 1 2 3 1 5 
The popped element is : 3
The array after popping is : 1 2 1 5 
The array after removing is : 2 1 5 
6. index():- This function returns the index of the first occurrence of value mentioned in arguments.7. reverse():- This function reverses the array.
Python3
# Python code to demonstrate the working of# index() and reverse()  # importing "array" for array operationsimport array  # initializing array with array values# initializes array with signed integersarr= array.array('i',[1, 2, 3, 1, 2, 5]) # printing original arrayprint ("The new created array is : ",end="")for i in range (0,6):    print (arr[i],end=" ") print ("\r") # using index() to print index of 1st occurrence of 2print ("The index of 1st occurrence of 2 is : ",end="")print (arr.index(2)) #using reverse() to reverse the arrayarr.reverse() # printing array after reversingprint ("The array after reversing is : ",end="")for i in range (0,6):    print (arr[i],end=" ")
Output: 
The new created array is : 1 2 3 1 2 5 
The index of 1st occurrence of 2 is : 1
The array after reversing is : 5 2 1 3 2 1
Reference : https://docs.python.org/3/library/array.html#module-arrayThis article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
2018umt1750
anikaseth98
amartyaghoshgfg
Python
School Programming
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Read a file line by line in Python
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
Python Dictionary
Inheritance in C++
Reverse a string in Java
C++ Classes and Objects
Interfaces in Java