Re: Dictionary is really not easy to handle

2016-04-28 Thread jfong
I was overwhelmed that three gurus inspire me in three different ways in their own flavour:-) That's really appreciated! Now I understand why it's so, thanks to all of you. To Peter: > With that information, can you predict what > for k, v in {(1, 2): "three"}: print(k, v) > will print?

Re: Dictionary is really not easy to handle

2016-04-28 Thread Steven D'Aprano
On Thu, 28 Apr 2016 06:27 pm, jf...@ms4.hinet.net wrote: > I have a dictionary like this: > dct ={1: 'D', 5: 'A', 2: 'B', 3: 'B', 4: 'E'} > > The following code works: > for k in dct: print(k, dct[k]) > ... > 1 D > 2 B > 3 B > 4 E > 5 A When you iterate over the dictionary, you get

Re: Dictionary is really not easy to handle

2016-04-28 Thread Peter Otten
jf...@ms4.hinet.net wrote: > I have a dictionary like this: > dct ={1: 'D', 5: 'A', 2: 'B', 3: 'B', 4: 'E'} > > The following code works: > But...this one? > for k,v in dct: print(k,v) > ... > Traceback (most recent call last): > File "", line 1, in > TypeError: 'int' object is

Re: Dictionary is really not easy to handle

2016-04-28 Thread Rustom Mody
On Thursday, April 28, 2016 at 1:57:40 PM UTC+5:30, jf...@ms4.hinet.net wrote: > I have a dictionary like this: > > >>> dct ={1: 'D', 5: 'A', 2: 'B', 3: 'B', 4: 'E'} > > The following code works: > > >>> for k in dct: print(k, dct[k]) > ... > 1 D > 2 B > 3 B > 4 E > 5 A > > and this one too: >

Dictionary is really not easy to handle

2016-04-28 Thread jfong
I have a dictionary like this: >>> dct ={1: 'D', 5: 'A', 2: 'B', 3: 'B', 4: 'E'} The following code works: >>> for k in dct: print(k, dct[k]) ... 1 D 2 B 3 B 4 E 5 A and this one too: >>> for k,v in dct.items(): print(k,v) ... 1 D 2 B 3 B 4 E 5 A But...this one? >>> for k,v in dct: