Python Forum
Help with Python dictionaries
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with Python dictionaries
#1
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?
Reply
#2
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
Reply
#3
(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'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
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?
Reply
#5
cars is clearly a dictionary. Why would you think there's a list there?
Reply
#6
It's a dictionary, and the values (think key:value) are dictionaries
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Dictionaries in Python erfanakbari1 1 2,142 Mar-02-2019, 06:40 PM
Last Post: ichabod801
  Newbie to Python - Problem in accessing Dictionaries and List sambill 1 3,063 Aug-17-2017, 07:38 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020