Prev: The 'dictionary' object | Next: Dictionary & List combinations
- Assigning pairs
- Calling the dict() constructor method
- dict comprehension
di = {}
di['ES'] = 'Spain'
di['GR'] = 'Greece'
di['IT'] = 'Italy'
di
alist = [('ES','Spain'),('GR','Greece'),('IT','Italy')]
di = dict(alist)
di
alist = [(chr(i),i) for i in range(65,70)]
di = dict(alist)
di
di = dict(ES='Spain',GR='Greece',IT='Italy')
di
code =['ES','GR','IT']
country = ['Spain','Greece','Italy']
di = dict(zip(code,country))
di
di = {<key expression:value expression> <'for' loop(s)>}
di = {x:x**2 for x in range(10)}
di
di = {str(x):x for x in range(5)}
di
di = {str(x*y):x+y for x in range(2) for y in range(3)}
di
di = {i if i%2==0 else 1:chr(x) if i%2==0 else 0 for i,x in enumerate(range(65,80,2))}
di
keys = ['a','b','c','d','e']
vals = ['A','B','C','D','E']
di = {keys[i]:vals[i] for i in range(5)}
di
di = {i : pow(i,2) for i in range(10)}
di
di = {i : pow(i,2) if i<=4 else pow(i,3) for i in range(10)}
di
di = {i if i<=4 else str(i) : pow(i,2) if i<=4 else pow(i,3) for i in range(10)}
di
di = {i:j for i,j in {1:'a', 2:'b'}.items()}
di
. Free learning material
. See full copyright and disclaimer notice