Seeking to populate a dictionary from a series of lists

Okay, well that kind of circles back to my first post; the structure of a dictionary, which is the first thing you’re going to need.

How do you see that structure? What key value pairs are you going to use?

Does it need to be searchable? That is to say, you have a number and it returns all related data? Or you have a name and it returns all related data? Or maybe it needs to do both?

Planning the data structure is important, if you don’t want to have to redesign it, because some factor has been missed or some feature needs to be added.

@rob42 It does not need to be searchable for the sake of this assignment. I need key value pairs of name: person’s name, idnumber: personsidnumber, wage: person’s wage.

I already have the lists of names, ids, and wages, I just need it properly formatted (sorted?) so that each person’s name, idnumber, wage, etc…is all right next to one another.

Okay, well the way I see that working is to have a dictionary of empty lists, than append the data from your lists, to the lists within the dictionary:

data = {'name' : [],
        'number' : [],
        'wage' : [],
        'benefitwage' : [],
        'raisedwage' : []
        }

Have you covered list objects and how they work?

{edit to add code}

Here you go, this should do what you need.

numbers = [212, 213]
names = ['bob', 'christie']
wages = [22.12, 24.15]
benefitwages = [24.63, 26.93]
raisedwages = [25.21, 26.73]

data = {'name' : [],
        'number' : [],
        'wage' : [],
        'benefitwage' : [],
        'raisedwage' : []
        }

for name in names:
    data['name'].append(name)
for number in numbers:
    data['number'].append(number)
for wage in wages:
    data['wage'].append(wage)
for benefitwage in benefitwages:
    data['benefitwage'].append(benefitwage)
for raisedwage in raisedwages:
    data['raisedwage'].append(raisedwage)

print(data)

Thank you Rob! And thank you everyone else as well!

No worries.

The data could be printed like this:

keys = data.keys()

print ("Index of names: ")
for index, name in enumerate(names):
    print (f"\t{numbers[index]} {name}")
name_index = int(input("\n> "))

if name_index not in numbers:
    print(f"No records for {name_index}")
else:
    data_numbers = data['number']
    name_index = data_numbers.index(name_index)
    for key in keys:
        for index in range(name_index,name_index+1):
            print(key, data.get(key)[index])

{edit for code update}

2 Likes