Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Convert list to Python array - Python
Next article icon

Appending elements to an array is a frequent operation andPython provides several ways to do it. Unlike lists, arrays are more compact and are designed for more efficient numerical computation. In this article, we will explore different methods for appending to an array.

Using append() Method

The simplest and most commonly used method to append an element to an array in Python is by usingappend() method. It’s straightforward and works in-place, meaning it modifies the original array directly.

Python
importarray# Create an arrayarr=array.array('i',[1,2,3])# Append an elementarr.append(4)print(arr)

Output
array('i', [1, 2, 3, 4])

This is generally the most efficient and simple method for appending one element to an array.

Let's take a look at other methods of appending to an array in python:

Using extend() Method

If you want to append multiple elements (i.e., extend the array with another iterable), theextend() method is your best choice. It appends all the elements from the iterable to the array.

Python
importarray# Create an arrayarr=array.array('i',[1,2,3])# Extend the array with multiple elementsarr.extend([4,5,6])print(arr)

Output
array('i', [1, 2, 3, 4, 5, 6])

This method is perfect for when you need to append multiple elements in a single operation.

Using Array Concatenation (+ Operator)

You can also append to an array by concatenating it with another array or iterable using the+ operator. This method creates a new array by combining the original array and the new elements.

Python
importarray# Create an arrayarr=array.array('i',[1,2,3])# Concatenate arraysarr=arr+array.array('i',[4,5])print(arr)

Output
array('i', [1, 2, 3, 4, 5])

While concatenation is simple and effective for combining arrays, it’s less efficient than extend() for appending multiple elements to an existing array.

Using a Loop

If you want to append multiple elements one by one, you can use a loop. Although this is less efficient than other methods, it gives you full control over how the elements are added.

Python
importarray# Create an arrayarr=array.array('i',[1,2,3])# Append elements using a loopforiinrange(4,7):arr.append(i)print(arr)

Output
array('i', [1, 2, 3, 4, 5, 6])

This method is more flexible but slower than using extend() when appending multiple elements.

Using fromlist() Method

The fromlist() method can be used to append elements from a list to an array. It's similar to extend(), but it’s specifically designed for lists and arrays.

Python
importarray# Create an arrayarr=array.array('i',[1,2,3])# Append elements from a list using fromlist()arr.fromlist([4,5,6])print(arr)

Output
array('i', [1, 2, 3, 4, 5, 6])

This method is great for appending from lists, but for general cases, extend() is often better.


Improve
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp