[Tutor] looping over a dictionary with tuple of 2 elements as value

Alan Gauld alan.gauld at yahoo.co.uk
Sun Jul 19 08:40:30 EDT 2020


On 19/07/2020 11:55, Manprit Singh wrote:
> Dear all,
> Consider a dictionary as given below :
> a = {"A" : (3, 1),  "B" : (4, 9)}
> Here in this dictionary for key "A" the value is tuple (3, 1), and for key
> "B" the value is tuple (4, 9) . Now for looping over this dictionary to get
> an output like this :
> A 3 1
> B 4 9
> 
> for i, (j, k) in a.items( ):
>     print(i,  j,  k)
> 
> Is this for loop syntactically correct ?

When it comes to syntax the best place to ask is the interpreter:

>>> a = {"A" : (3, 1),  "B" : (4, 9)}
>>> for i, (j, k) in a.items( ):
	print(i,j,k)

	
A 3 1
B 4 9
>>>

So yes, it is syntactically correct.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list