0

i have two 2d arrays and i want to iterate through them both and store the values in a new array for example:

days = [["monday", "11:00"],
        ["monday", "13:00"],
        ["tuesday", "10:00"]] 

and my other array:

modules = [["chemistry", "alkenes"],
           ["biology", "alcohols"],
           ["biology, cells"]]

i want the new arrays output to be

["monday", "11:00", "Chemistry", "alkenes"] 

currently my code looks likes this:

for x in days:
    newarray = []
    for i in modules:
        newarray.append(x)
        newarray.append(i)

but this doesn't work so how would i create this new array

martineau
  • 119,623
  • 25
  • 170
  • 301
taz19
  • 1
  • 1
  • There is an error in the final list of modules. It should be `["biology", "cells"]` not `["biology, cells"]` – Prince Mar 20 '22 at 13:13

1 Answers1

2

This code should solve your problem

days = [["monday", "11:00"],
        ["monday", "13:00"],
        ["tuesday", "10:00"]] 

modules = [["chemistry", "alkenes"],
           ["biology", "alcohols"],
           ["biology", "cells"]]

new_arr = []
for i in range(len(days)):
    merged_arr = []
    merged_arr.append(days[i][0])
    merged_arr.append(days[i][1])
    merged_arr.append(modules[i][0])
    merged_arr.append(modules[i][1])
    new_arr.append(merged_arr)
    
for i in new_arr:
    print(i)
LoopFree
  • 139
  • 2