w3resource

Python: Append a new item to the end of the array


2. Append a New Item to the End of an Array

Write a Python program to append a new item to the end of the array.

Pictorial Presentation:

Python Exercises: Append a new item to the end of the array

Sample Solution:

Python Code :

from array import *
array_num = array('i', [1, 3, 5, 7, 9])
print("Original array: "+str(array_num))
print("Append 11 at the end of the array:")
array_num.append(11)
print("New array: "+str(array_num))

Sample Output:

Original array: array('i', [1, 3, 5, 7, 9])                            
Append 11 at the end of the array:                                     
New array: array('i', [1, 3, 5, 7, 9, 11])

For more Practice: Solve these Related Problems:

  • Write a Python program to append a single integer to an array and then print the updated array.
  • Write a Python program to use a loop to append multiple items to an array one by one.
  • Write a Python program to extend an array with elements from a list using the extend() method and display the result.
  • Write a Python program to merge two arrays by appending the second array's elements to the first and then print the merged array.

Python Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Python program to create an array of 5 integers and display the array items. Access individual element through indexes.
Next: Write a Python program to reverse the order of the items in the array.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.