Array in Python | Set 1 (Introduction and Features)

Array operations:

1. array (data type, list of values) : — This function is used to create an array with the data type and list of values ​​specified in its arguments. Some of the data types are mentioned in the table below.

int

-wp- inline-f74fb0de8d71e0cc53e95b25025d2853 “> signed int

td class = “amp-wp-inline-2f0158eb062d1ac553a7edcb8a744628″> unsigned long

4

-wp-inline-2f0158eb062d1ac553a7edcb8a744628 “> int

`f`
Type Code C Type Python Type Minimum size in Bytes
`b` signed char

1
`B` td>

unsigned char int 1
`u` unicode character 2 >

`h` signed short 2
2
unsigned short

= “amp-wp-inline-2f0158eb062d1ac553a7edcb8a744628″> 2

`i` int
`I` unsigned intd intd = “amp-wp-inline-2f0158eb062d1ac553a7edcb8a744628″> int 2
`l` signed long 4
`L`
int
`q` int 8
`Q` unsigned long long 8
float `d` = ” amp-wp-inline-2f 0158eb062d1ac553a7edcb8a744628 “> double float

2. append () : — This function is used to add the value specified in its arguments to the end of an array.

3. insert (i, x) : — This function is used to add a value to the position specified in its argument.

# Python code to demonstrate how it works
# array (), append (), insert ()

 
# import & quot; array & quot; for array operations

import array

 
# initializes array with array values ​​
# initializes signed array integers

arr = array.array ( `i` , [ 1 , 2 , 3 ]) 

 
# print the original array

print ( "The new created array is : " , end = " " )

for i in range ( 0 , 3 ):

print (arr [i], end = "" )

 

print ( "" )

  
# using append () to insert the new value into end

arr.append ( 4 ) ;

 
# print the added array

print ( "The appended array is:" , end = "")

for i in range ( 0 , 4 ):

print (arr [i], end = "" )

 
# using insert () to insert a value at a specific position
# inserts 5 at the 2nd position

arr.insert ( 2 , 5 )

 

print ( "" )

 
# print array after insertion

print ( " 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 specified in its argument and returns it.

5. remove () : — This function is used to remove the first occurrence of the value specified in its arguments.

# Python code to demonstrate how it works
# pop () and remove ()

 
# import & quot; array & quot; for array operations

import array

 
# initializes array with array values ​​
# initializes signed array integers

arr = array.array ( `i` , [ 1 , 2 , 3 , 1 , 5 ]) 

 
# print the original array

print ( "The new created array is : " , end = " ")

for i in range ( 0 , 5 ):

print (arr [i], end = "" )

 

print ( "" )

 
# using pop () to remove element at 2nd position

print ( " The popped element is: " , end = " ")

print (arr.pop ( 2 ));

 
# print array after it appears

print ( "The array after popping is:" , end = "")

for i in range ( 0 , 4 ):

print (arr [i], end = "" )

  

print ( "" )

  
# using remove () to remove 1st occurrences 1

arr.remove ( 1 )

 
# print array after deletion

print ( "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 the value specified in the arguments.

7. reverse () : — This function flips the array.

# Python code to demonstrate how it works
# index () and reverse ()

 
# import & quot; array & quot; for array operations

import array

 
# initializes array with array values ​​
# initializes signed array integers

arr = array.array ( `i` , [ 1 , 2 , 3 , 1 , 2 , 5 ]) 

 
# print the original array

print ( " The new created array is: " , end = "")

for i in range ( 0 , 6 ):

print (arr [i], end = "" )

 

print ( " " )

 
# using index () to print the first occurrence index 2

print ( " The index of 1st occurrence of 2 is: " , end = " ")

print (arr.index ( 2 ))

 
# using reverse () for reverse array
arr.reverse ()

 
# print array after reverse

print ( "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 

Link:
https://docs.python.org/3/library/ array.html # module-array

This article courtesy of Manjit Singh .If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.