1

I am searching if values from list 1 are available in list2 or not. if whatever values from list 1 are found in list2 then I want to replace all those values in list2 by a single value.

list1 = ['banana', 'apple', 'cat', 'peacock']
list2 = ['hello', 'apple', 'cat', 'sherrif']

Solution I tried:

for i,item in enumerate(list2):
  if item in list1:
    list2[i]= 'cat'

print(list2)

Current output: replaced every value in list2 by jackie

list2 = ['hello', 'jackie', 'jackie', 'sherrif']

Expected: Output: As values like apple, cat from list1 are available in list2 so in output, both should be replaced by single value jackie. if no value found list1 found in list2 then list2 remains same

list2 = ['hello', 'jackie', 'sherrif']
3
  • Unclear. What happens if list2=['banana', 'apple', 'peacock', 'cat'] ? Do you replace it with ['banana', 'jackie', 'peacock', 'jackie'] or ['banana', 'jackie', 'peacock']? In other words, do you replace only the first value that is also contained in list1 and remove all the reset, or do you replace each consecutive sequence of values also contained in list1 with a separate "jackie"? Commented Jul 13, 2020 at 14:16
  • do you want the order of the list to be maintained? Commented Jul 13, 2020 at 14:16
  • Note that item in list1 is O(len(list1)) since it searches all elements of list1. The total algorithm becomes O(len(list1)*len(list2)), which is OK if either one of the lists are small, but becomes a disaster if both lists are big. Solution: create set1=set(list1) and work with that. Commented Jul 13, 2020 at 14:30

3 Answers 3

2

Try this:

Changed = False
for x in list1:
    if x in list2:
        if not Changed:
            list2[list2.index(x)] = "jackie"
            Changed = True
        else:
            list2.remove(x)
Sign up to request clarification or add additional context in comments.

1 Comment

O(n ** 2) complexity. OK for very small list (<10), very bad for big lists (>1000)
1

This can be done with a list comprehension and a little 'trick' to remove duplicates:

print(list(dict.fromkeys(['Jacky' if el in list1 else el for el in list2])))

The list comprehension should be clear: Add 'Jacky' if the element in list2 occurs in list1, else add the element itself.

Then we create a dictionary using the list items as keys. This automatically deletes duplicates, and finally we convert it back to a list:

['hello', 'Jacky', 'sherrif']

Following the comments below:

IF it is mandatory that duplicates in list2 are preserved AND the order is important and you don't want to or cannot rely on the order in a dictionary, use this instead:

list3 = []
[list3.append(el) for el in ['Jacky' if el in list1 else el for el in list2] if el!='Jacky' or 'Jacky' not in list3]
print(list3)

3 Comments

In this answer: 1. The order is not guaranteed to be preserved. 2. It loses duplicates, which are perfectly legal, and should be maintained if they are not also contained in list1.
First: I believe the order is guaranteed because nowadays dictionaries preserve the order in which keys are added. Second: you're right ;-) though the OP was not specific about that case.
you are correct only as of python-3.7. For python-3.6, or earlier, there is no guarantee regarding the iteration order of dict elements
0

I have made a small function that does what you want and returns at the end both lists. It checks whether any element of list_1 is present in the list_2 if it is true, the whole list_2 is changed by the list comprehension into a list containing the word Jackie

list1 = ['banana', 'apple', 'cat', 'peacock']
list2 = ['hello', 'apple', 'cat', 'sherrif']

def low_func(list_1, list_2):
    for i in list_1:
        if i in list_2:
            list2 = ['jackie' for x in list_2]
    return list1, list2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.