Aug-07-2019, 05:25 PM
temp is a dict. A dict can have any hashable object as a key, including strings. temp['name'] = name assigns the value associated with the variable name to the key 'name' in temp. Stepping through the code:
vlans_list = [ 'vlan 10', 'name DATA', 'vlan 20', 'name VOICE', 'vlan 30', 'name WIRELESS' ] vlans = [] for item in vlans_list: # loop through vlans_list, assigning each member to item if 'vlan' in item: # check for vlan being in the string temp = {} # create a new dictionary id = item.strip().strip('vlan').strip() # remove vlan from the string, assign the result to id temp['id'] = id # store id in the new dictionary (key = 'id') elif 'name' in item: # check for name being in the string name = item.strip().strip('name').strip() # remove 'name' from the string, assign it to variable name temp['name'] = name # store name in the dictionary. vlans.append(temp) # store the dictionary with the vlan info in a listEach vlan ends up with it's own dictionary in the list vlans.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures