Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
.difference()
The tool .difference() returns a set with all the elements from the set that are not in an iterable.
Sometimes the – operator is used in place of the .difference() tool, but it only operates on the set of elements in set.
Set is immutable to the .difference() operation (or the – operation).
>>> s = set("Hacker")
>>> print s.difference("Rank")
set(['c', 'r', 'e', 'H'])
>>> print s.difference(set(['R', 'a', 'n', 'k']))
set(['c', 'r', 'e', 'H'])
>>> print s.difference(['R', 'a', 'n', 'k'])
set(['c', 'r', 'e', 'H'])
>>> print s.difference(enumerate(['R', 'a', 'n', 'k']))
set(['a', 'c', 'r', 'e', 'H', 'k'])
>>> print s.difference({"Rank":1})
set(['a', 'c', 'e', 'H', 'k', 'r'])
>>> s - set("Rank")
set(['H', 'c', 'r', 'e'])
Students of District College have a subscription to English and French newspapers. Some students have subscribed to only the English newspaper, some have subscribed to only the French newspaper, and some have subscribed to both newspapers.
You are given two sets of student roll numbers. One set has subscribed to the English newspaper, and one set has subscribed to the French newspaper. Your task is to find the total number of students who have subscribed to only English newspapers.
The first line contains the number of students who have subscribed to the English newspaper.
The second line contains the space separated list of student roll numbers who have subscribed to the English newspaper.
The third line contains the number of students who have subscribed to the French newspaper.
The fourth line contains the space separated list of student roll numbers who have subscribed to the French newspaper.
Output the total number of students who are subscribed to the English newspaper only.
9
1 2 3 4 5 6 7 8 9
9
10 1 2 3 11 21 55 6 8
4
The roll numbers of students who only have English newspaper subscriptions are:
4, 5, 7 and 9.
Hence, the total is 4 students.
eng = set()
fre = set()
n = raw_input()
for i in raw_input().split(' '):
eng.add(i)
m = raw_input()
for i in raw_input().split(' '):
fre.add(i)
sol = eng.difference(fre)
print len(sol)
# Enter your code here. Read input from STDIN. Print output to STDOUT
n1 = int(input())
set_1 = set(map(int,input().split()))
n2 = int(input())
set_2 = set(map(int,input().split()))
print(len(set_1-set_2))
# Enter your code here. Read input from STDIN. Print output to STDOUT
n = input()
s = set(map(int, raw_input().split()))
m = input()
t = set(map(int, raw_input().split()))
print len(s.difference(t))
# Enter your code here. Read input from STDIN. Print output to STDOUT
n1=input()
li1 = input().split()
s1 = set(li1)
n2=input()
li2 = input().split()
s2 = set(li2)
s1s2 = s1.difference(s2)
print(len(s1s2))
In our experience, we suggest you solve this Set .difference() Hacker Rank Solution and gain some new skills from Professionals completely free and we assure you will be worth it.
The Set .difference() is available on Hacker Rank for Free, if you are stuck anywhere between compilation, just visit Queslers to get all Hacker Rank Solution
I hope this Set .difference() Hacker Rank Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Hacker Rank, Leetcode, Codechef, Codeforce Solution.
This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.
Keep Learning!
More Hacker Rank Problem & Solutions >>
itertools.product() Hacker Rank Solution
String Validators Hacker Rank Solution
Text Alignment Hacker Rank solution