Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
YiChen Chen
5,228 PointsI don't understand why this can invert key and value of dictionary
Dic1 = {'aa': 0, 'bb': 1, 'cc': 2, 'dd': 3, 'ee': 4}
Dic2 = {}
for k,v in Dic.items():
Dic2[v]=k
Dic2 = {0: 'aa', 1: 'bb', 2: 'cc', 3: 'dd', 4: 'ee'}
I don't know what it means Dic2[v]=k
1 Answer
Andreas cormack
Python Web Development Techdegree Graduate 33,011 PointsHi YiChen
# first there is a typo i think it should be Dic1.items() and not Dic.items
for k,v in Dic1.items(): # k stands for key and v stands for value
Dic2[v]= k # this line makes the value of Dic1 the key of Dic2 and key of Dic1 the value of Dic2
hope this helps