283 views
1 1 vote

Task: You have two lists of IDs. You need to find all IDs that are present in either list but not in both (the symmetric difference).

List 1: $[101,102,103,104]$

List 2: $[103,104,105,106]$

Expected Result:
\[
\{101,102,105,106\}
\]

Which Python set operation or method achieves this?

  1. $\texttt{set(list1)&set(list2)} $
     
  2. $\texttt{set(list1).union(list2)}$
     
  3. $\texttt{set(list1)}$ ^  $\texttt{set(list2)}$
     
  4. $\texttt{set(list1).intersection(list2)}$

2 Answers

0 0 votes
The XOR Operator ( ^ ): In Python, the caret symbol ( ^ ) is the shorthand operator for the symmetric_difference() method. It compares two sets and returns a new set containing elements found in either set1 or set2, but not in their intersection.
Answer:
Position:
Show:

Related questions

1 1 vote
1 1 answer
227
227 views
GO Classes asked Jan 28
227 views
You are performing a NON-RECURSIVE (iterative) traversal of a Binary Tree using a single STACK. You use the following logic:1. Push the Root to the Stack.2. While the sta...
0 0 votes
1 1 answer
217
217 views
GO Classes asked Jan 28
217 views
Consider a Directed Acyclic Graph (DAG) with $n$ vertices and $e$ edges. If you perform a Topological Sort on this graph, which of the following is true?THERE IS ALWAYS E...
3 3 votes
1 1 answer
238
238 views
GO Classes asked Jan 28
238 views
In Python, what is the result of True ^ True?True False $1$ TypeError
0 0 votes
1 1 answer
207
207 views
GO Classes asked Jan 28
207 views
You are given a string. You need to create a \textbf{dictionary} where the keys are the characters and the values are the number of times that character appears in the st...