Friday, May 22, 2020

Dictionaries

Dictionaries

Dictionaries have key value pairs separated by commas inside a pair of {}.


rooms = {
  "living": "TV",
  "dining": "eat",
  "family": 4
}

In order to get the value associated with the key just call the dictionary with square braces with the key argument or you can use the get() method on the dictionary. Either way if the key is not found the returned value is None.

x = rooms["family"]
print(x)
# result is 4

print(rooms.get("family"))  # gives 4


If duplicate keys exist the most recent down the dictionary listing is recognized and the others are ignored.

rooms = {
  "living": "TV",
  "dining": "eat",
  "dining": 4
} # gives 4

If you want to loop through keys only,  have the loop's index dummy variable going as the argument call to the dictionary variable.

If you want to loop through the values only use the get() values method with no index inside the parentheses.

If you want to loop through both the keys and values use the item() method and 2 dummy loop variables.

When using 2 dummy loop variables with item() curious things happen if you leave off one of the dummy variables and or choose not to print one of the dummies.

case 1 - provide two dummy variables x,y to loop, print y, but do not print x

rooms = {
  "living": "TV",
  "dining": "eat",
  "family": 4
}
for x, y in rooms.items():
  print(y)

# results in values unquoted on a line by itself

TV
eat
4


case 2 - only one loop variable y and print it

rooms = {
  "living": "TV",
  "dining": "eat",
  "family": 4
}
for y in rooms.items():
  print(y)

# results in key-value tuples, one for each pair. so that a single loop variable forces key-value association to go unbroken.

('living', 'TV')
('dining', 'eat')
('family', '4')

Case 3 - provide two dummy variables x,y to loop, only print x, do not print y

rooms = {
  "living": "TV",
  "dining": "eat",
  "family": 4
}
for x, y in rooms.items():
  print(x)

# results in keys unquoted on a line by itself
living
dining
family


case 4, the normal case

rooms = {
  "living": "TV",
  "dining": "eat",
  "family": 4
}
for x, y in room.items():
  print(x, y)


# results in key-value pairs, each pair unquoted on a line by itself, not as tuples
living TV
dining eat
family 4

Remove an item:  pop(key) or the popitem() will get the rightmost one. You can also use del to remove either one key or the entire dictionary.

Dictionary assignments are by reference, therefore, to make a true copy only use the copy() method or the dictionary Constructor dict. Note when using a dictionary Constructor the keywords are not entered as string literals and an equal sign replaces colons. Another feature of the dict constructor is that it will fail if you have repeated keys. This is unlike constructing a dictionary by hand where duplicate keys are allowed.

rooms = dict(living= "TV", dining= "eat", family= 4)

and make second one as a copy:

newrooms = dict(rooms) # a true copy, with a different memory address

morerooms = newrooms

# NOT a true copy, because newrooms and morerooms share the same memory space. They # connected by reference; a change to one makes the other one change right away the same # way.

Dictionaries can be nested arbitrarily, in fact, you can create separate dictionaries and then place them in yet another dictionary by name. 


jones = {
  "dad" : {
    "name" : "John",
    "year" : 45
  },
  "mom" : {
    "name" : "Sarah",
    "year" : 43
  },
  "son" : {
    "name" : "Justin",
    "year" : 15
  } # notice no comma for the last one
}

or you can create each person as its own separate dictionary, and use those dictionary variables as elements in a new dictionary that holds them all.


dad = {
  "name" : "John",
  "year" : 45
}

mom = {
  "name" : "Sarah",
  "year" : 43
}

son = {
  "name" : "Justin",
  "year" : 15
}

jonesfamily = {
  "father" : dad,
  "mother" : mom,
  "child " : son 
# notice no comma for the last one
}




No comments:

Post a Comment