Academic Work Personal
|
Python /
Basic Datatypes: Dictionaries
aa = {} # an empty dictionary dict = {'alpha':3, 'beta':4, 'gamma':5, 'delta':7} # getting value print 'beta is:', dict['beta'] # beta is: 4 # add an entry dict['XXX'] = 10 print dict # {'alpha': 3, 'beta': 4, 'XXX': 10, 'gamma': 5, 'delta': 7} # delete an entry del dict['alpha'] print dict # {'beta': 4, 'XXX': 10, 'gamma': 5, 'delta': 7} # print keys, print values print dict.keys() # ['beta', 'XXX', 'gamma', 'delta'] print dict.values() # [4, 10, 5, 7] # check if an entry is there print 'beta is there:', dict.has_key('beta') # True print 'alpha is there:', dict.has_key('alpha') # False (it was removed) # iterate the dict. and access the key/value pairs myDict = {'alpha':3, 'beta':4, 'gamma':5, 'delta':7} for k, v in myDict.iteritems(): print k, v dict.clear() # delete all items in the dict. dict = {} # another way to delete all items List comprehension with dictionariesdata = {'location': 'death star', \ 'hero': 'luke skywalker', \ 'objective': 'save princess leia'} li = ["%s=%s" % (k, v) for (k, v) in data.items()] print li # ['objective=save princess leia', 'hero=luke skywalker', 'location=death star'] Some dictionary functions
|
![]() anime | bash | blogs | bsd | c/c++ | c64 | calc | comics | convert | cube | del.icio.us | digg | east | eBooks | egeszseg | elite | firefox | flash | fun | games | gimp | google | groovy | hardware | hit&run | howto | java | javascript | knife | lang | latex | liferay | linux | lovecraft | magyar | maths | movies | music | p2p | perl | pdf | photoshop | php | pmwiki | prog | python | radio | recept | rts | scala | scene | sci-fi | scripting | security | shell | space | súlyos | telephone | torrente | translate | ubuntu | vim | wallpapers | webutils | wikis | windows Blogs and Dev. * Ubuntu Incident Places Debrecen | France | Hungary | Montreal | Nancy Notes Hobby Projects * Jabba's Codes Quick Links [ edit ] |