Compare Two Lists and Find the Unique Values

Comparing two Lists and returning the distinct values and the differences

var A = new List<string>() { "A", "B", "C", "D" };
var B = new List<string>() { "A", "E", "F", "G" };

A.Except(B).ToList()
// outputs List<string>(2) { "B", "C", "D" }
B.Except(A).ToList()
// outputs List<string>(2) { "E", "F", "G" }
B.Intersect(A).ToList()
// outputs List<string>(2) { "A" }

Compare two list and get unique elements from first list Python

result = [i for i in A if i not in B]

Is there any way to find unique values between two lists without using a loop in dart

You can use where() with contains() methods from List:

void main() {
List<String> first = ['A','B','C','D'];
List<String> second = ['B','D'];

List<String> result = first.where((item) => !second.contains(item)).toList();
print(result); // [A, C]
}

Edit in DartPad.

Python Compare two lists and check for two unique strings

First you can find number of matches using list comprehension and then len > 2 or not

>>> num = 2
>>> l = [i for i in strings if i in file]
>>> if len(l) >= num:
print('found')
found

unique values between 2 lists

After all the hassle with closing and re-opening I feel someone ought to actually answer the question.

There are different ways to achieve the desired result:

  1. List comprehensions: [i for i in f if i not in x]. Maybe less efficient but preserves order. Credit goes to Chris_Rands (comment above).

  2. Set operations: set(f) - set(x). Likely more efficient for larger lists but does not preserve order. Gredit goes to mpf82. This also removes duplicates in f, as pointed out by asongtoruin.



Related Topics



Leave a reply



Submit