python - Why is a tuple of tuples of length 1 not actually a tuple unless I add a comma? -
given tuple of tuples t:
(('a', 'b')) and individual tuple t1:
('a','b') why does:
t1 in t return false?
update: ipython:
in [22]: t = (('a','b')) in [23]: t1 = ('a','b') in [24]: t1 in t out[24]: false and how check tuple in tuple?
the problem because t not tuple of tuples, tuple. comma makes tuple, not parentheses. should be:
>>> t = (('a','b'),) >>> t1 = ('a', 'b') >>> t1 in t true in fact, can loose outer parentheses:
>>> t = ('a','b'), >>> t1 = 'a','b' >>> type(t) <type 'tuple'> >>> type(t[0]) <type 'tuple'> >>> type(t1) <type 'tuple'> >>> t1 in t true although needed precedence, if in doubt put them in. remember, comma makes tuple.
Comments
Post a Comment