OneBite.Dev - Coding blog in a bite size

extract a sub-array from an array in python

Code snippet on how to extract a sub-array from an array in python

  my_array = [5, 8, 12, 65, 22, 16]
  # Define sub-array
  sub_array = [my_array[1], my_array[2], my_array[4]]
  
  # Print sub-array
  print(sub_array)

This code creates an array called my_array containing 6 numbers, then defines a sub-array called sub_array containing my_array’s second, third, and fifth elements. Finally, it prints the contents of sub_array to the console. By running this code, we should get the output [8, 12, 22].

python