Change language

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 4
’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.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically