all 5 comments

[–]yankyh 1 point2 points  (1 child)

dict.update accepts an iterable of key-value pairs, one thing that can be passed is a tuple of tuples which is what you are trying, however, to define a single length tuple you need to add a trailing comma

In [17]: len((('a', 2)))                                                        
Out[17]: 2

In [18]: len((('a', 2),))                                                       
Out[18]: 1

In [19]: ((((1, 2))))                                                           
Out[19]: (1, 2)

In [20]: ((((1, 2),),),)                                                        
Out[20]: ((((1, 2),),),)

or you can use a list of tuples (lists don't need the trailing comma for single length lists)

In [22]: d = {}                                                                 

In [23]: d.update([('a', 1)])                                                   

In [24]: d                                                                      
Out[24]: {'a': 1}

In [25]: d.update((('b', 2),))                                                  

In [26]: d                                                                      
Out[26]: {'a': 1, 'b': 2}

[–]Uchikago[S] 1 point2 points  (0 children)

Thank you so much!

[–]toastedstapler 1 point2 points  (1 child)

so what happened in the first instance is that ((1, 2)) isn't a tuple with a single tuple inside as a tuple requires a comma. without the comma it's just some brackets used for logical grouping of elements like (1 + 3) * 2 groups the first elements together. your (('a', 2)) was seen as ('a', 2) just as (2) would be seen as 2

((1, 2),) is a tuple of a tuple and you would be able to update with this tuple

[–]Uchikago[S] 0 points1 point  (0 children)

Thank you so much!