1

Comparison between these two lists and eliminating from List1 if it matches. Is there any way to process the List1 too.

List1: ["'file',", "'ist',", "'customer',"]

List2: ['permission', 'ist', 'dr']
4
  • What do you mean by process the list? Commented May 25, 2018 at 17:16
  • 1
    Can you post your expected output? Commented May 25, 2018 at 17:16
  • processing List means List1 to ['file', 'ist', 'customer'] to make processing easier. @S Commented May 25, 2018 at 17:27
  • expected output should eliminate 'ist' from list1 Commented May 25, 2018 at 17:28

2 Answers 2

5

Seems like a simple list comprehension would do it.

filtered_list = [string for string in List1 if string not in List2]

Warning: your strings in List1 don't match the format of the strings in List2. Not sure if that was your intent. The string 'ist', won't match with the string ist.

Sign up to request clarification or add additional context in comments.

4 Comments

for what it's worth this isn't the most efficient as it's O(n^2) running time
actually i know to do comparison, the format of the list1 is creating problem. How to handle that.
The cleanest approach would probably be creating a new list with the strings cleaned up. str.strip might do it, depending on what other formatting errors are in those strings. cleaned_list_1 = [x.strip(['"', "'", ',']) for x in List1]
@Tnguyen True. It would run in O(n) if he used set(List2) and searched in the set instead of in List2 directly.
2

This will give you the required output.

for i in list(List1):
    if i.strip("',") in List2:
        List1.remove(i)

1 Comment

You are just so awesome.. was stuck at it from morning.. brilliant!! thanks!!

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.