Python Array extend() Method

The extend() method in Python is used to extend an array by appending elements from an iterable (e.g., list, another array, etc.). This method is useful for combining arrays or adding multiple elements to an array in a single operation.

Table of Contents

  1. Introduction
  2. Importing the array Module
  3. extend() Method Syntax
  4. Understanding extend()
  5. Examples
    • Basic Usage
    • Extending an Array with Another Array
    • Extending an Array with a List
  6. Real-World Use Case
  7. Conclusion

Introduction

The extend() method is a built-in method for array objects in Python, which are provided by the array module. This method appends elements from an iterable to the end of the array, modifying the original array.

Importing the array Module

Before using the extend() method, you need to import the array module, which provides the array object.

import array

extend() Method Syntax

The syntax for the extend() method is as follows:

array.extend(iterable)

Parameters:

  • iterable: An iterable (e.g., list, another array) containing elements to be appended to the array.

Returns:

  • None. The method modifies the array in place.

Understanding extend()

The extend() method takes an iterable as an argument and appends each element from the iterable to the end of the array. This method is useful for combining arrays or adding multiple elements at once.

Examples

Basic Usage

To demonstrate the basic usage of extend(), we will create an array and extend it with another array.

Example

import array

# Creating an array of integers
arr = array.array('i', [1, 2, 3])

# Extending the array with another array
arr.extend(array.array('i', [4, 5, 6]))

# Printing the extended array
print("Extended array:", arr)

Output:

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

Extending an Array with Another Array

This example shows how to extend an array with elements from another array.

Example

import array

# Creating two arrays of integers
arr1 = array.array('i', [10, 20, 30])
arr2 = array.array('i', [40, 50, 60])

# Extending the first array with the second array
arr1.extend(arr2)

# Printing the extended array
print("Array after extending with another array:", arr1)

Output:

Array after extending with another array: array('i', [10, 20, 30, 40, 50, 60])

Extending an Array with a List

This example shows how to extend an array with elements from a list.

Example

import array

# Creating an array of integers
arr = array.array('i', [100, 200, 300])

# Extending the array with a list
arr.extend([400, 500, 600])

# Printing the extended array
print("Array after extending with a list:", arr)

Output:

Array after extending with a list: array('i', [100, 200, 300, 400, 500, 600])

Real-World Use Case

Merging Data from Multiple Sources

In real-world applications, the extend() method can be used to merge data from multiple sources, such as combining sensor readings or aggregating user input.

Example

import array

# Simulating sensor readings from multiple sensors
sensor1_readings = array.array('f', [23.5, 24.1, 22.8])
sensor2_readings = array.array('f', [24.0, 23.9, 22.5])

# Merging sensor readings into a single array
all_readings = array.array('f')
all_readings.extend(sensor1_readings)
all_readings.extend(sensor2_readings)

# Printing the merged sensor readings
print("All sensor readings:", all_readings)

Output:

All sensor readings: array('f', [23.5, 24.100000381469727, 22.799999237060547, 24.0, 23.899999618530273, 22.5])

Combining User Input

The extend() method can also be used to combine user input collected at different times into a single array.

Example

import array

# Collecting user input at different times
inputs1 = array.array('u', 'hello')
inputs2 = array.array('u', 'world')

# Combining user input into a single array
all_inputs = array.array('u')
all_inputs.extend(inputs1)
all_inputs.extend(inputs2)

# Printing the combined user input
print("Combined user input:", all_inputs)

Output:

Combined user input: array('u', 'helloworld')

Conclusion

The extend() method in Python is used for appending elements from an iterable to an array. This method is essential for combining arrays or adding multiple elements in a single operation. By using the extend() method, you can efficiently manage and grow arrays in various applications, such as merging data from multiple sources or combining user input. Understanding how to use the extend() method effectively allows you to handle array operations with ease.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare