Skip to content
Advertisement

How to append elements from a multi-dimensional list into 2nd multi-dimensional list

I am trying to append elements from a multi-dimensional list into a 2nd multi-dimensional list. Here is the code I wrote –

my_list = [[['b', ['a'], 'c'], ['d', ['a', 'b'], 'e']], 
           [['j', ['a', 'f']], ['q', 't']]]
ref_list = [[['q', 'w', 't'], ['y', 'u']], 
            [['s', 'o'], ['p', 'k', 'l']]]

newlist = []
for num, i in enumerate(my_list): 
  small_list = [] 
  for num_1, j in enumerate(i): 
    semilist = []
    for k in j:        
      if isinstance(k, list):      
        onelist = []
        for a in k:
          for ii in ref_list[num][num_1]:
            onelist.append(ii)           
        semilist.append(onelist)
      else:
        semilist.append(k)
    small_list.append(semilist)
  newlist.append(small_list)
      
print(newlist)

The output I am getting is –

[[['b', ['q', 'w', 't'], 'c'], ['d', ['y', 'u', 'y', 'u'], 'e']], [['j', ['s', 'o', 's', 'o']], ['q', 't']]]

The output I am looking for is of sort –

[[['b', ['q', 'w', 't'], 'c'], ['d', ['y', 'u'], 'e']], [['j', ['s', 'o']], ['p', 'k', 'l']]]

I want to replace elements of innermost list of “my_list” with the elements of the “ref_list”.

Advertisement

Answer

Try:

my_list = [
    [["b", ["a"], "c"], ["d", ["a", "b"], "e"]],
    [["j", ["a", "f"]], ["q", "t"]],
]

ref_list = [[["q", "w", "t"], ["y", "u"]], [["s", "o"], ["p", "k", "l"]]]


for a, b in zip(my_list, ref_list):
    for i, subl in enumerate(a):
        for ii, v in enumerate(subl):
            if isinstance(v, list):
                subl[ii] = b[i]
                break
        else:
            a[i] = b[i]

print(my_list)

Prints:

[
    [["b", ["q", "w", "t"], "c"], ["d", ["y", "u"], "e"]],
    [["j", ["s", "o"]], ["p", "k", "l"]],
]

EDIT: To append elements from 2nd list instead replacing:

my_list = [
    [["b", ["a"], "c"], ["d", ["a", "b"], "e"]],
    [["j", ["a", "f"]], ["q", "t"]],
]

ref_list = [[["q", "w", "t"], ["y", "u"]], [["s", "o"], ["p", "k", "l"]]]


for a, b in zip(my_list, ref_list):
    for i, subl in enumerate(a):
        for ii, v in enumerate(subl):
            if isinstance(v, list):
                subl[ii].extend(b[i])
                break
        else:
            a[i].extend(b[i])

print(my_list)

Prints:

[
    [["b", ["a", "q", "w", "t"], "c"], ["d", ["a", "b", "y", "u"], "e"]],
    [["j", ["a", "f", "s", "o"]], ["q", "t", "p", "k", "l"]],
]

EDIT2: To add elements of 2nd list at the beginning of first list:

my_list = [
    [["b", ["a"], "c"], ["d", ["a", "b"], "e"]],
    [["j", ["a", "f"]], ["q", "t"]],
]

ref_list = [[["q", "w", "t"], ["y", "u"]], [["s", "o"], ["p", "k", "l"]]]


for a, b in zip(my_list, ref_list):
    for i, subl in enumerate(a):
        for ii, v in enumerate(subl):
            if isinstance(v, list):
                subl[ii] = b[i] + subl[ii]
                break
        else:
            a[i] = b[i] + a[i]

print(my_list)

Prints:

[
    [["b", ["q", "w", "t", "a"], "c"], ["d", ["y", "u", "a", "b"], "e"]],
    [["j", ["s", "o", "a", "f"]], ["p", "k", "l", "q", "t"]],
]
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement