Seeking to populate a dictionary from a series of lists

I am seeking to populate a new dictionary from a series of lists. These separate lists contain information to specific categories like employee name, employee wage, employee ID number, etc. I have previously used “append” to add to lists, but I believe that this dictionary needs to be in “order” for my assignment. I am not sure how to create a dictionary and populate it in order so that employee names, wages, ID numbers all are sorted properly with each list in the dictionary pertaining to each employee?

Sorry if this is a noobish question, I have only been learning python for a few weeks and my class is apparently brutal to new programmers. Any help is much appreciated.

EDIT:

I am supposed to combine the different lists into a “database like structure” using a dictionary.

For the sake of simplicity I will use just two values for each list to start with as I can manage the rest:

number = [212, 213]
name = [bob, christie]
wage = [22.12, 24.15]
benefitwage = [24.63, 26.93]
raisedwage = [25.21, 26.73]

The values within each list are in order, I just need a dictionary that shows bob has employee number 212, wage 22.12, benefitwage 24.63, christie has number 213, wage 24.15…etc. etc.

To answer your question, we need to see some sample data, and the format that you expect to build.

Please keep it as small as possible, we don’t want to be drowned by hundreds of lines of data if only ten will do :slight_smile:

In addition to the above post, you’ll need to know the structure of a dictionary, how the items are added, retrieved , removed, and changed and some terminology, such as what a key value pair is and what that means.

Without that basic knowledge, you’ll not be able to understand any advice that is offered.

If I understood, I think it’s quite easy:

names = [...]
wages = [...]
ids = [...]

result = []

for name, wage, id in zip(names, wages, ids):
    tmp = {"wage": wage, "name": name, "id": id}
    result.append(tmp)

@rob42 @steven.daprano

Thank you for the replies. I am supposed to combine the different lists into a “database like structure” using a dictionary.

For the sake of simplicity I will use just two values for each list to start with as I can manage the rest:

number = [212, 213]
name = [bob, christie]
wage = [22.12, 24.15]
benefitwage = [24.63, 26.93]
raisedwage = [25.21, 26.73]

The values within each list are in order, I just need a dictionary that shows bob has employee number 212, wage 22.12, benefitwage 24.63, christie has number 213, wage 24.15…etc. etc.

There is a limitation to the commands/functions I am allowed to use. I know I can use the get and append commands. I believe the approach is to make an empty dictionary and then populate from these lists?

What is database like structure using dictionary? Does this qualify:

[{'number': 212, 'name': 'bob', 'wage': 22.12, 'benefitwage': 24.63, 'raisedwage': 25.21},
 {'number': 213, 'name': 'christie', 'wage': 24.15, 'benefitwage': 26.93, 'raisedwage': 26.73}]

@aivarpaalberg Yes, that qualifies!

@Marco_Sulla

I believe this may be correct, I just need to make sure each employeee and associated information are in order.

@Marco_Sulla and @negativespace247

Note: id() is a Python function. It’s not good practice (in fact, it’s strongly discouraged) to use any Python reserved words, as names for your variables, within your own scripts. So, always ‘name check’ these things. If you’re using a good enough IDE, then it’s very likely that any reserved words will be color changed and easy to spot.

2 Likes

@negativespace247: well, they are in order. The dicts are putted in a list.

@rob42: you’re not wrong, you said the best practice. But in real world I use id() only in tests, so I simply don’t care…

Fair enough, but it may be better to not post that kind of usage, as it will encourage those with less experience, into bad practice, which is something you should care about.

@rob42 IMHO the “bad practice” was to put a so common word as a builtin function that, furthermore, it’s misused by users the majority of times :smiley:

PS: I agree with you with all the other builtin functions.

names = […]
wages = […]
ids = […]

result =

for name, wage, id in names, wages, ids:
tmp = {“wage”: wage, “name”: name, “id”: id}
result.append(tmp)

I tried using this but I am getting a value error “too many values to unpack expected 3”

@Marco_Sulla

For the sake of simply understanding the commands and syntax I am only using name, number and wage at the moment. I believe I copied your sample code correctly yet it is still throwing this error?

Or do you mean that because I have multiple lists in the dictionary I need to add more code even though I am just looking at names/wages/id numbers at the moment?

Sorry, I forgot zip. I’ll edit the original answer

I actually do not believe I am allowed to use zip as it has not been taught in my class yet.

Aaaaaaaah homework… X-D

2 Likes

I know lol. Is it possible to use .get to do something similar?

I need to understand: are you constructing (or coding) a dictionary object and then populating it? Or are you going to use the dict() function to return the dictionary object, as in, for example:

car = dict(brand = "Ford", model = "Mustang", year = 1964)

I believe I am constructing a dictionary and then populating it using the lists I have as the source.