Reputation: 21162
I use python only occasionally, sorry for a seemingly trivial question
>>> a = set(((1,1),(1,6),(6,1),(6,6)))
>>> a
set([(6, 1), (1, 6), (1, 1), (6, 6)])
>>> a - set(((1,1)))
set([(6, 1), (1, 6), (1, 1), (6, 6)])
>>> a.remove((1,1))
>>> a
set([(6, 1), (1, 6), (6, 6)])
why '-
' operator didn't delete the element but the remove
did ?
Upvotes: 1
Views: 136
Reputation: 114481
You missed a comma when trying to specify a tuple of one element. Syntax for tuples is indeed somewhat tricky...
Some examples
w = 1, 2, 3 # creates a tuple, no parenthesis needed
w2 = (1, 2, 3) # works too, like x+y is the same as (x+y)
x, y, z = w # unpacks a tuple
k0 = () # creates an empty tuple
k1 = (1,) # a tuple with one element (note the comma)
k = (1) # just a number, NOT a tuple
foo(1, 2, 3) # call passing three numbers, not a tuple
bar((1, 2, 3)) # call passing a tuple
if x in 1, 2: # syntax error, parenthesis are needed
pass
for x in 1, 2: # ok here
pass
gen = (x for x in 1, 2) # error, parenthesis needed here around (1, 2)
Upvotes: 2
Reputation: 33397
Because you missed a comma:
>>> set(((1,1)))
set([1])
should be:
>>> set(((1,1),))
set([(1, 1)])
or, to be more readable:
set([(1,1)])
or even (Py2.7+):
{(1,1)}
Upvotes: 8