You can use sets
t1 = [ (1,2), (3,4), (5,6), (7,8), (9,10), (11,12) ]
t2 = [ (3,4), (11,12) ]
set(t2).issubset(t1)
# returns true
# or equivalent use '<=' so
set(t2) <= set(t1)
# returns true
Answer from Finn on Stack OverflowYou can use sets
t1 = [ (1,2), (3,4), (5,6), (7,8), (9,10), (11,12) ]
t2 = [ (3,4), (11,12) ]
set(t2).issubset(t1)
# returns true
# or equivalent use '<=' so
set(t2) <= set(t1)
# returns true
For simplicity, you could do this:
print all(x in t1 for x in t2)
However, that's going to search through t1 for each element in t2. That probably doesn't matter when t1 is small as in this case, but to allow for larger collections I would do this:
s1 = set(t1)
print all(x in s1 for x in t2)
or this:
print set(t1).issuperset(t2)
This will generally be faster, since in is much faster for sets than for large lists. There's no major performance benefit in converting t2 to a set, regardless of size, so I wouldn't.
As always, it's better if you get your data in the "right" collection to begin with. So if the main purpose of t1 is to look things up in it, use a set in the first place rather than a list.
how to check if an element of a nested list appears in another list?
python - Can I check if a list contains any item from another list? - Stack Overflow
How do I check if a list contains all the elements of another list in Python? It's not working - Stack Overflow
Find all items from one list that are not in another list
Here's what I've got so far:
list1 = ["a", ["b", "c"], ["d", "e"]] list2 = ["b", "c", "f"] check = any(item in list1 for item in list2) print(check)
How would I check whether or not list2 contains an element of list1, including one of the nested lists? It returns True if I put "a" in list2, but gives False as it is right now. I'm pretty sure this is because it's checking single items, but I don't know how to make it work.
Any help would be greatly appreciated!
Using any() along with a generator expression:
list1 = ['item1','item2','item3']
list2 = ['item4','item5','item3']
if any(x in list1 for x in list2):
print("Duplicates found.")
else:
print("No duplicates found.")
You could use a set. isdisjoint is a method which returns True if two sets have nothing in common and False if the sets overlap. After converting list1 and list2 into sets, they both contain 'item3' so isdisjoint returns False.
set(list1).isdisjoint(set(list2))
>>> list1 = ['item1','item2','item3']
>>> list2 = ['item4','item5','item3']
>>> set(list1).isdisjoint(set(list2))
False
You can combine this with the not operator to do what you want.
list1 = ['item1','item2','item3']
list2 = ['item4','item5','item3']
if not set(list1).isdisjoint(set(list2)):
print("Duplicates found.")
else:
print("No duplicates found.")
I think you want all:
list1 = ['Gryffindor', 'Ravenclaw', 'Hufflepuff', 'Slytherin']
list2 = ['Gryffindor', 'Ravenclaw']
checkif = all(item in list1 for item in list2)
Also, you need to swap list1 and list1 to get the result you describe in the print line.
You can use sets here and specifically check the relationship whether one of your sets is a subset of the other:
set(list2).issubset(list1) # True
To use this the first object must be a set hence set(list2) but the second can be any iterable. One caveat here is that since we're comparing sets, it will only check for unique elements, i.e. it will not care about repeated values.
Hello all,
I am trying to create a list (list3) of all items that are exist in one list (list1), but do not exist in another list (list2). Any help would be greatly appreciated!
I am using pandas / data frames to create two lists each made of the items in a column in different excel files and I want to identify the items in list1 that are not found in list2.
Basically I have:
Df1 = pd.read_excel(file1)
Df2 = pd.read_excel(file2)
List1 = df1['Column Name']
List2 = df2['Column Name']
List3 = [x for x in List1 if x not in List2]
This is basically just recreating List1 and not omitting the entries that exist in List2.
Thank you all in advance! Please let me know if you need more info
If all items are unique, you can use sets.
>>> items = set([-1, 0, 1, 2])
>>> set([1, 2]).issubset(items)
True
>>> set([1, 3]).issubset(items)
False
There's an all() and any() function to do this.
To check if big contains ALL elements in small
result = all(elem in big for elem in small)
To check if small contains ANY elements in big
result = any(elem in big for elem in small)
the variable result would be boolean (TRUE/FALSE).