Python Forum
Help with Python dictionaries - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Help with Python dictionaries (/thread-31598.html)



Help with Python dictionaries - kam_uk - Dec-21-2020

Hi

I have a list of employees in various file formats, but they all have the same descriptions for the data,

EmployeeID - this is unique
FirstName
LastName
Telephone
Email
Address
City
State
Country

The formats I have are Excel CSV, txt file and json (employee.csv, employee.txt, employee.json)

What I want to do is have all this in one file (combined_employees)

Questions:

1. I understand dictionaries follow the format of key:value, but I've never worked with multiple dictionaries using the same unique ID (in this case EmployeeID). How would I arrange the data?

2. What's the best way to convert these all into one data set? What it be a list of dictionaries?


RE: Help with Python dictionaries - jefsummers - Dec-22-2020

It depends of course on what you want to do with it.
For persistent storage, I would use an SQL database.
For short term manipulation I would use a Pandas dataframe.
In both cases I would have the rows represent the employees, with the columns being the field names


RE: Help with Python dictionaries - perfringo - Dec-22-2020

(Dec-21-2020, 11:26 PM)kam_uk Wrote: Questions:

1. I understand dictionaries follow the format of key:value, but I've never worked with multiple dictionaries using the same unique ID (in this case EmployeeID). How would I arrange the data?

2. What's the best way to convert these all into one data set? What it be a list of dictionaries?

"the same unique ID" is somewhat confusing sequence of words.

However, the question seems to be what data structure is most suitable for task at hand. This is under homework section so I assume that assignment consists some terms and conditions regarding that. These should be followed.

In case there is no requirements then anything would do which works - it is homework after all Wink .

On more serious note - making choice is pending on several factors like number of records and how this data will be used. If it will be *always* queried by EmployeeID then dictionary where EmployeeID is key and data associated with it is value is good design choice. Lookup is as fast as it can be. However, if data will be mainly queried based on other fields or aggregated for reporting purposes (based on City, State or Country for example) then list of dictionaries could be OK. If data will not change, then immutable namedtuple could be used instead of dictionary.

As of arrangement of data. Suppose we have cars which data we want to keep in dictionary. Cars have unique property VIN (Vehicle Identity Number). For our purposes we have four number VIN (actual is 17 characters long and includes both digits and capital letters). So dictionary with one record and simple queries on that could look like this:

>>> cars = {4242: {'brand': 'Volvo', 'model': 'XC90', 'color': 'white'}}
>>> cars[4242]
{'brand': 'Volvo', 'model': 'XC90', 'color': 'white'}
>>> cars[4242]['brand']
'Volvo'



RE: Help with Python dictionaries - kam_uk - Dec-22-2020

Thanks!

So using your example, the best data structure would be similar to the below?

The 001, 002 represent the VIN (equivalent to my EmployeeID)

cars = {
    '001': {'brand': 'volvo', 'model': 'XC90', 'colour': 'red'},
    '002': {'brand': 'toyota', 'model': 'AV1', 'colour': 'blue'},
}
Is this called a dictionary or list of dictionaries?


RE: Help with Python dictionaries - ndc85430 - Dec-22-2020

cars is clearly a dictionary. Why would you think there's a list there?


RE: Help with Python dictionaries - jefsummers - Dec-22-2020

It's a dictionary, and the values (think key:value) are dictionaries