Python add to Array With Examples

Python doesn’t have any specific data type as an array. We can use List that has all the characteristics of an array.

Python array module can be used to create an array of integers and floating-point numbers.

If you want to do some mathematical operations on an array, you should use the NumPy module.

1. Python add to Array

  • If you are using List as an array, you can use its append(), insert(), and extend() functions. You can read more about it at Python add to List.
  • If you are using array module, you can use the concatenation using the + operator, append(), insert(), and extend() functions to add elements to the array.
  • If you are using NumPy arrays, use the append() and insert() function.

2. Adding elements to an Array using array module

  • Using + operator: a new array is returned with the elements from both the arrays.
  • append(): adds the element to the end of the array.
  • insert(): inserts the element before the given index of the array.
  • extend(): used to append the given array elements to this array.

3. Adding elements to the NumPy Array

  • append(): the given values are added to the end of the array. If the axis is not provided, then the arrays are flattened before appending.
  • insert(): used to insert values at the given index. We can insert elements based on the axis, otherwise, the elements will be flattened before the insert operation.

4. References

By admin

Leave a Reply