how to add elements to an array in python code example

Example 1: append element to an array python

x = ['Red', 'Blue']
x.append('Yellow')

Example 2: append item to array python

data = []
data.append("Item")

print(data)

Example 3: python add element to array

my_list = []

my_list.append(12)

Example 4: python array[:

import array as arr

numbers_list = [2, 5, 62, 5, 42, 52, 48, 5]
numbers_array = arr.array('i', numbers_list)

print(numbers_array[2:5]) # 3rd to 5th
print(numbers_array[:-5]) # beginning to 4th
print(numbers_array[5:])  # 6th to end
print(numbers_array[:])   # beginning to end

Example 5: append element an array in python

my_input = ['Engineering', 'Medical'] 
my_input.append('Science') 
print(my_input)