Home>

I wrote the following code, which looks for the missing element in the list, and when found, adds this element to the end of the list:

massive1= ['a','b', 'c', 'd']
massive2= ['b','e','d']
massive3=[]
massive3= massive1
for i in massive2:
    if i not in massive3:
      massive3.append(i)
print(*massive3)

But I want it to add that missing element before the value that it failed to validate, so that the rest of the elements are shifted to the right. I tried using the .insert function, but it gave an error, so I ask for help

First, your massive3 reference points to the massive1 array, is that what you really wanted? Second, it's not clear what behavior you're expecting. If the element is not found anywhere in the array, where do you want to insert it, at what position? To a position after the element that was previously found, i.e. 'e' needs to be inserted after 'b' turns out (it was found earlier)?

CrazyElf2022-02-14 10:00:10

@CrazyElf If a similar element is not found anywhere in the array, then the element we want to insert simply goes to the end of the array. And about the link, I want to first fill massive3 with the largest array, that is, massive1, and then compare the resulting array with massive2

Ulcus_Molle2022-02-14 10:08:29

Can not understand anything. If you add to the end of the array, then why then do I want to make it add this missing element before the value with which there was a mismatch during the check. What is the actual task?

MBo2022-02-14 10:15:39

@Ulcus_Molle That is, if an element is found, then it still needs to be inserted into the array? You already decide -an element that is not found is added, or an element that is found is also added (in what position? Again it is not clear)

CrazyElf2022-02-14 10:21:27

@MBo We need to merge two arrays into one array so that there are no duplicate elements and the order of both arrays is preserved as much as possible. So when you combine the arrays ['a','b', 'c', 'd'] and ['b','e','d'] you should get an array ['a','b', 'c' , 'e', ​​'d']

Ulcus_Molle2022-02-14 10:22:24