Python Arrays

In this Python tutorial we will learn about Python Arrays and we will also cover different examples related to Python Sets.

  • Python array
  • Creating an array
  • Adding elements to a array
  • Accessing elements from the array
  • Removing elements from the array

Python array

In Python array is a group of items supply the neighbouring memory location. It can store the multiple items of the identical type. The array can make it simple to compute the position of each element.

Array can hold in python by simply using a module called as an array. It can be very helpful when we have to operate only a particular data type values.

So, with this, we have learned about the array which can be done in the python.

Creating an array

Array can be created in python by simply imported an array module. The array() function is used to create an array with data type in its argument.

Example:

In the following example we are creating an array and the array is created by simply using an array() function.

# Python program to demonstrate Create of Array
 
# Import array library
import array as arr
 
# creating an array with an integer type
m = arr.array('i', [2, 4, 6])
 
# printing an original array
print ("New created array is : ", end =" ")
for i in range (0, 3):
    print (m[i], end =" ")
print()
 
# creating an array with double type
n = arr.array('d', [3.5, 4.2, 4.3])
 
# printing original array
print ("New created array is : ", end =" ")
for i in range (0, 3):
    print (n[i], end =" ")

Output:

After running the above code we get the following output in which we can see that the after using the array() function array is created.

Python array
Python array

Github Link

Check this code in Repository from Github and you can also fork this code.

Github User Name: PythonT-Point

Adding elements to a array

In python the elements can be added by simply using the insert() function. Insert function is used to add one or more data elements to an array.

Example:

In the following example we are adding the elements to an elements by simply using the insert() function.

  • m = arr.array(‘i’, [2, 4, 6,8]) is used as array with the int type.
  • print (“Array before insertion : “, end =” “) is used to print the array before insertion.
  • m.insert(2, 5) is used to inserting an array using insert() function.
  • n = arr.array(‘d’, [3.5, 4.2, 5.3,6.4]) is used an array with float type.
  • n.append(5.4) is used as adding an array using append() function.
# Python program to demonstrate Add elements to an Array
 
# importing an "array" for array creations
import array as arr
 
# array with int type
m = arr.array('i', [2, 4, 6,8])
 
 
print ("Array before insertion : ", end =" ")
for i in range (0, 4):
    print (m[i], end =" ")
print()
 
# inserting array using insert() function
m.insert(2, 5)
 
print ("Array after insertion : ", end =" ")
for x in (m):
    print (x, end =" ")
print()
 
# array with float type
n = arr.array('d', [3.5, 4.2, 5.3,6.4])
 
print ("Array before insertion : ", end =" ")
for x in range (0, 4):
    print (n[x], end =" ")
print()
 
# adding an element using append()
n.append(5.4)
 
print ("Array after insertion : ", end =" ")
for x in (n):
    print (x, end =" ")
print()

Output:

In the following output you can see that the array is inserting using the insert() function and append() function.

Adding elements to a array in python
Adding elements to a array in python

Read: Python Sets

Accessing elements from the array

In Python the elements of the array can be access to the index number. The index[] operator is used to access an item in an array.

Example:

In the following code we will accessing the elements from the array by using the index[] operator.

  • m = ary.array(‘i’, [2, 4, 6, 8, 10, 12]) is used an array with int type.
  • print(“Accessing the element is: “, m[1]) is used to print an accessing elements of array.
  • n = ary.array(‘d’, [3.5, 4.2, 5.3,6.7]) is used as an array with float type.
# accessing of element from list and importing array module
import array as ary
 
# array with int type
m = ary.array('i', [2, 4, 6, 8, 10, 12])
 
# accessing element of array
print("Accessing the element is: ", m[1])
 
# accessing element of array
print("Accessing the element is: ", m[4])
 
# array with float type
n = ary.array('d', [3.5, 4.2, 5.3,6.7])
 
# accessing element of array
print("Accessing the element is: ", n[2])
 
# accessing element of array
print("Accessing the element is: ", n[3])

Output:

After running the above code we get the following output in which we can see that the elements of an array can be access to the index number.

Accessing element from the array in python
Accessing element from the array in python

Read:Python Dictionary

Removing elements from the array

In Python for removing the elements the remove() function is used. The remove() only remove one element at a time.

Here the remove() function in the list only remove the second event of the searched element.

Example:

In the following code we will remove the element from the array by using the remove() function.

  • m = array.array(‘i’, [2, 4, 6, 8, 10,12]) is used to initializing the array with the array values.
  • print (m.pop(3)) is used as to remove element at the 3rd position.
  • m.remove(2) is used remove() to remove 2nd occurrence of 2.
# Python program to demonstrate the removal of elements in a Array
  
# importing "array" for array operations
import array
  
# initializing array with array values
m = array.array('i', [2, 4, 6, 8, 10,12])
 
# printing the original array
print ("The new created array is : ", end ="")
for i in range (0, 6):
    print (m[i], end =" ")
 
print ("\r")
 
# using pop() to remove element at 3rd position
print ("The pop element is : ", end ="")
print (m.pop(3))
 
# printing array after popping
print ("The array after popping is : ", end ="")
for i in range (0, 5):
    print (m[i], end =" ")
 
print("\r")
 
# using remove() to remove 2nd occurrence of 2
m.remove(2)
 
# printing array after removing
print ("The array after removing is : ", end ="")
for i in range (0, 4):
    print (m[i], end =" ")

Output:

After running the above code we get the following output in which we can see that the element are popped and then after popping removing the elements from the array.

Removing element from the array
Removing element from the array

So, in this tutorial, we have learned about the Python Arrays and we have covered all these topics.

  • Python array
  • Creating an array
  • Adding elements to a array
  • Accessing elements from the array
  • Removing elements from the array

Do follow the following tutorials also:

33 thoughts on “Python Arrays”

  1. Cool blog! Is your theme custom made or did you download it from somewhere? A design like yours with a few simple tweeks would really make my blog jump out. Please let me know where you got your design. Many thanks

    Reply
  2. I like what you guys are up too. Such clever work and reporting! Carry on the excellent works guys I have incorporated you guys to my blogroll. I think it’ll improve the value of my site 🙂

    Reply
  3. I was recommended this blog by my cousin. I’m no longer positive whether or not this post is written by way of him as no one else know such particular approximately my problem. You’re wonderful! Thanks!

    Reply
  4. I’m extremely inspired along with your writing abilities as smartly as with the structure for your blog. Is that this a paid theme or did you modify it yourself? Either way stay up the excellent quality writing, it’s uncommon to peer a nice blog like this one today..

    Reply
  5. Your work has captivated me just as much as it has you. The sketch you’ve created is tasteful, and the material you’ve written is impressive. However, you seem anxious about the prospect of presenting something that could be considered questionable. I believe you’ll be able to rectify this situation in a timely manner.

    Reply
  6. I loved as much as you will receive carried out right here The sketch is tasteful your authored subject matter stylish nonetheless you command get got an edginess over that you wish be delivering the following unwell unquestionably come further formerly again as exactly the same nearly very often inside case you shield this hike

    Reply
  7. Hi i think that i saw you visited my web site thus i came to Return the favore Im attempting to find things to enhance my siteI suppose its ok to use a few of your ideas

    Reply
  8. The degree of my fascination with your creations is equal to your own enthusiasm. The sketch is tasteful, and the authored material is of a high caliber. Yet, you appear uneasy about the prospect of heading in a direction that could cause unease. I’m confident you’ll be able to resolve this situation efficiently.

    Reply
  9. helloI like your writing very so much proportion we keep up a correspondence extra approximately your post on AOL I need an expert in this space to unravel my problem May be that is you Taking a look forward to see you

    Reply
  10. Simply desire to say your article is as surprising The clearness in your post is simply excellent and i could assume you are an expert on this subject Fine with your permission let me to grab your feed to keep up to date with forthcoming post Thanks a million and please carry on the gratifying work

    Reply
  11. you are truly a just right webmaster The site loading speed is incredible It kind of feels that youre doing any distinctive trick In addition The contents are masterwork you have done a great activity in this matter

    Reply
  12. Fran Candelera You’re so awesome! I don’t believe I have read a single thing like that before. So great to find someone with some original thoughts on this topic. Really.. thank you for starting this up. This website is something that is needed on the internet, someone with a little originality!

    Reply
  13. Your writing is not only informative but also incredibly inspiring. You have a knack for sparking curiosity and encouraging critical thinking. Thank you for being such a positive influence!

    Reply

Leave a Comment