Building a HashMap using Python in a loop. Clarification needed -


i running in loop , and parsing string. string contains 2 items host , application running on host. expected host runs multiple applications. i'd store in 1 data structure host used key.

below failed attempt. please me understand why last element being saved in host = app format.

what expect see host = app1, app2 etc see host = app2 (always last)   data = dict()  def add(line):     l = line.split("/")     host = l[0].strip()     app = l[-1].strip()      data[host].append(app)  entry in env:     if "/" not in entry: continue     add(entry)  print data 

the problem data[host].append(app)

would append value of app list (or similar) stored data[host]

when run this, value of data[host] not set, , can't append it. keyerror. perhaps meant data[host] = app? or...

try:     data[host].append(app) except keyerror:     data[host] = [app] 

Comments