<jupyter_start><jupyter_text>Array  An array is a data structure that stores values of same data type. In Python, this is the main difference betweenarrays and lists.While python lists can contain values corresponding to different data types, arrays in python can only containvalues corresponding to same data type. In this tutorial, we will understand the Python arrays with few examples.If you are new to Python, get started with the Python Introduction article.To use arrays in python language, you need to import the standard array module. This is because array is not afundamental data type like strings, integer etc. Here is how you can import array module in python : from array import * Once you have imported the array module, you can declare an array. Here is how you do it: arrayIdentifierName = array(typecode, [Initializers])<jupyter_code>my_array = array('i', [1,2,3,4,5])
print(my_array[1])

print(my_array[2])

print(my_array[0])
from array import *
my_array = array('i', [1,2,3,4,5])
for i in my_array:
    print(i)<jupyter_output>1
2
3
4
5<jupyter_text>* Append any value using append() method<jupyter_code>my_array = array('i', [1,2,3,4,5])
my_array.append(6)
print(my_array)<jupyter_output>array('i', [1, 2, 3, 4, 5, 6])<jupyter_text>* Insert value using insert() method<jupyter_code>my_array = array('i', [1,2,3,4,5])
my_array.insert(0,0)
print(my_array)<jupyter_output>array('i', [0, 1, 2, 3, 4, 5])<jupyter_text>* Extend value using extend() method<jupyter_code>my_array = array('i', [1,2,3,4,5])
my_extnd_array = array('i', [7,8,9,10])
my_array.extend(my_extnd_array)
print(my_array)<jupyter_output>array('i', [1, 2, 3, 4, 5, 7, 8, 9, 10])<jupyter_text>* Add items from list into array using fromlist()<jupyter_code>my_array = array('i', [1,2,3,4,5])
c=[11,12,13]
my_array.fromlist(c)
print(my_array)<jupyter_output>array('i', [1, 2, 3, 4, 5, 11, 12, 13])<jupyter_text>* Remove any array element using remove() method<jupyter_code>my_array = array('i', [1,2,3,4,5])
my_array.remove(4)
print(my_array)<jupyter_output>array('i', [1, 2, 3, 5])<jupyter_text>* Remove last array element using pop() method<jupyter_code>my_array = array('i', [1,2,3,4,5])
my_array.pop()
print(my_array)<jupyter_output>array('i', [1, 2, 3, 4])<jupyter_text>* Fetch any element through its index using index()method<jupyter_code>my_array = array('i', [1,2,3,4,5])
print(my_array.index(5))

my_array = array('i', [1,2,3,3,5])
print(my_array.index(3))<jupyter_output>4
2<jupyter_text>* Reverse a python array using reverse() method<jupyter_code>my_array = array('i', [1,2,3,4,5])
my_array.reverse()
my_array<jupyter_output><empty_output><jupyter_text>* Get array buer information throughbuer_info() method<jupyter_code>my_array = array('i', [1,2,3,4,5])
my_array.buffer_info()<jupyter_output><empty_output><jupyter_text>* Check for number of occurrences of an elementusing count() method<jupyter_code>my_array = array('i', [1,2,3,3,5])
my_array.count(3)<jupyter_output><empty_output><jupyter_text>* Convert array to a python list with sameelements using tolist() method<jupyter_code>my_array = array('i', [1,2,3,4,5])
c = my_array.tolist()
c<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup