Extend() function of array class

Method Name:

extend()

Signature:

extend(iterable)

Return Value:

None

Exceptions:

Type Error – when the Python iterable or the array object passed in is not same as the base type of the array.

Overview:

  • An array object can be added with more members from a Python iterable or from another array by calling the extend() method and passing in an array or iterable.

Example:

# An example Python program that extends a Python array

# with elements from a Python iterable

import array

 

# Create an array

chars = array.array('B');

 

# Array with ASCII code for small letters

for val in range(97, 123):

     chars.append(val);

    

print("ASCII code for small letters:"); 

print(chars); 

 

# Create a Python list   

caps = [];

for val in range (65, 91):

     caps.append(val);

 

# Extend the array to include ASCII code for small letters

chars.extend(caps);

print("ASCII code for small and capital letters:");

print(chars);

 

Output:

ASCII code for small letters:

array('B', [97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122])

ASCII code for small and capital letters:

array('B', [97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90])


Copyright 2024 © pythontic.com