HOME - Recent Changes - Search:

Academic Work


Personal

* pot de départ


dblp


(:twitter:)

-----

[ edit | logout ]
[ help | sandbox | passwd ]

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 dictionaries

data = {'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

clearD.clear() → None. Remove all items from D.
copyD.copy() → a shallow copy of D
fromkeysdict.fromkeys(S[,v]) → New dict with keys from S and values equal to v. v defaults to None.
getD.get(k[,d]) → D[k] if k in D, else d. d defaults to None.
has_keyD.has_key(k) → True if D has a key k, else False
itemsD.items() → list of D's (key, value) pairs, as 2-tuples
iteritemsD.iteritems() → an iterator over the (key, value) items of D
iterkeysD.iterkeys() → an iterator over the keys of D
itervaluesD.itervalues() → an iterator over the values of D
keysD.keys() → list of D's keys
popD.pop(k[,d]) → v, remove specified key and return the corresponding value. If key is not found, d is returned if given, otherwise KeyError is raised
popitemD.popitem() → (k, v), remove and return some (key, value) pair as a 2-tuple; but raise KeyError if D is empty.
setdefaultD.setdefault(k[,d]) → D.get(k,d), also set D[k]=d if k not in D
updateD.update(E, **F) → None. Update D from dict/iterable E and F. If E has a .keys() method, does: for k in E: D[k] = E[k]. If E lacks .keys() method, does: for (k, v) in E: D[k] = v. In either case, this is followed by: for k in F: D[k] = F[k]
valuesD.values() → list of D's values
Cloud City


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
* Python Adventures
* me @ GitHub


Places

Debrecen | France | Hungary | Montreal | Nancy


Notes

full circle | km


Hobby Projects

* Jabba's Codes
* PmWiki
* Firefox
* PHP
* JavaScript
* Scriptorium
* Tutorials
* me @ GitHub


Quick Links


[ edit ]

View - Edit - History - Attach - Print *** Report - Recent Changes - Search
Page last modified on 2009 October 20, 22:55