ktl014
7/21/2017 - 4:50 PM

how to make dictionary; how to add initial values; how to insert/update values; how to merge 2 dictionaries; how to delete items in dici

how to make dictionary; how to add initial values; how to insert/update values; how to merge 2 dictionaries; how to delete items in dicitonary; how to access key/values; From https://stackoverflow.com/questions/1024847/add-new-keys-to-a-dictionary

for name, age in mydict.items():
    if age == search_age:
        print name
### Making a dictionary ###

data = {}
# OR
data = dict()

### Initially adding values ###

data = {'a':1,'b':2,'c':3}
# OR
data = dict(a=1, b=2, c=3)

### Inserting/Updating value ###

data['a']=1  # updates if 'a' exists, else adds 'a'
# OR
data.update({'a':1})
# OR
data.update(dict(a=1))
# OR
data.update(a=1)

### Merging 2 dictionaries ###

data.update(data2)  # Where data2 is also a dict.

### Deleting items in dictionary ###

del data[key] #Remove specific element in a dictionary
data.pop(key) #Removes the key & returns the value
data.clear() #Clear entire dictionary