Bob Smith Bob Smith - 1 year ago 78
Python Question

Multiple values per key (array or dictionary)?

I'm designing a very simple database for products I have in a mock warehouse.

There is a code (the key), a description (value), number of units available per pack (value), individual prices (value) and the total cost of the pack (value):

Product Number | Description | Pack | Individual Cost | Total
01234567 | Black Ballpoint Pen | 4 | 0.50 | 2.00
12345670 | Football | 2 | 4.50 | 9.00

The product is the key and the other attributes are all values.

I tried to use a dictionary structure but that didn't work as it didn't have a fixed number of fields.

I haven't seen an example as such that involves more than one field.
I haven't seen an array or dictionary example that has more than one field to store all of this data.

I want it something like:

{01234567:("Black Ballpoint Pen", 4, 0.50, 2.00), 12345670:(Football, 2, 4.50, 9.00)}

Is this possible?

Answer Source

I want it something like:

{01234567:("Black Ballpoint Pen", 4, 0.50, 2.00), 12345670:(Football,2, 4.50, 9.00)}

Did you try it? All you have to do is change Football to "Football", and it works as expected:

>>> i = {01234567:("Black Ballpoint Pen", 4, 0.50, 2.00), 12345670:("Football",2, 4.50, 9.00)}
>>> i[01234567]
('Black Ballpoint Pen', 4, 0.5, 2.0)

A dictionary can hold any kind of value - lists, strings, numbers, tuples and even other dictionaries.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download