Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 782701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:25:40+00:00 2026-05-14T20:25:40+00:00

I have a Excel CSV files with employee records in them. Something like this:

  • 0

I have a Excel CSV files with employee records in them. Something like this:

mail,first_name,surname,employee_id,manager_id,telephone_number
blah@blah.com,john,smith,503422,503423,+65(2)3423-2433
foo@blah.com,george,brown,503097,503098,+65(2)3423-9782
....

I’m using DictReader to put this into a nested dictionary:

import csv
gd_extract = csv.DictReader(open('filename 20100331 original.csv'), dialect='excel')
employees = dict([(row['employee_id'], row) for row in gp_extract])

Is the above the proper way to do it – it does work, but is it the Right Way? Something more efficient? Also, the funny thing is, in IDLE, if I try to print out “employees” at the shell, it seems to cause IDLE to crash (there’s approximately 1051 rows).

2. Remove employee_id from inner dict

The second issue issue, I’m putting it into a dictionary indexed by employee_id, with the value as a nested dictionary of all the values – however, employee_id is also a key:value inside the nested dictionary, which is a bit redundant? Is there any way to exclude it from the inner dictionary?

3. Manipulate data in comprehension

Thirdly, we need do some manipulations to the imported data – for example, all the phone numbers are in the wrong format, so we need to do some regex there. Also, we need to convert manager_id to an actual manager’s name, and their email address. Most managers are in the same file, while others are in an external_contractors CSV, which is similar but not quite the same format – I can import that to a separate dict though.

Are these two items things that can be done within the single list comprehension, or should I use a for loop? Or does multiple comprehensions work? (sample code would be really awesome here). Or is there a smarter way in Python do it?

Cheers,
Victor

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T20:25:41+00:00Added an answer on May 14, 2026 at 8:25 pm

    Your first part has one simple issue (which might not even be an issue). You don’t handle key collisions at all (unless you intend to simply overwrite).

    >>> dict([('a', 'b'), ('a', 'c')])
    {'a': 'c'}
    

    If you’re guaranteed that employee_id is unique, there isn’t an issue though.

    2) Sure you can exclude it, but no real harm done. Actually, especially in python, if employee_id is a string or int (or some other primitive), the inner dict’s reference and the key actually reference the same thing. They both point to the same spot in memory. The only duplication is in the reference (which isn’t that big). If you’re worried about memory consumption, you probably don’t have to.

    3) Don’t try to do too much in one list comprehension. Just use a for loop after the first list comprehension.

    To sum it all up, it sounds like you’re really worried about the performance of iterating over the loop twice. Don’t worry about performance initially. Performance problems come from algorithm problems, not specific language constructs like for loops vs list comprehensions.

    If you’re familiar with Big O notation, the list comprehension and for loop after (if you decide to do that) both have a Big O of O(n). Add them together and you get O(2n), but as we know from Big O notation, we can simplify that to O(n). I’ve over simplified a lot here, but the point is, you really don’t need to worry.

    If there are performance concerns, raise them after you written the code and prove it to yourself with a code profiler.

    response to comments

    As for your #2 reply, python really doesn’t have a lot of mechanisms for making one liners cute and extra snazzy. It’s meant to force you into simply writing the code out vs sticking it all in one line. That being said, it’s still possible to do quite a bit of work in one line. My suggestion is to not worry about how much code you can stick in one line. Python looks a lot more beautiful (IMO) when its written out, not jammed in one line.

    As for your #1 reply, you could try something like this:

    employees = {}
    for row in gd_extract:
        if row['employee_id'] in employees:
            ... handle duplicates in employees dictionary ...
        else:
            employees[row['employee_id']] = row
    

    As for your #3 reply, not sure what you’re looking for and what about the telephone numbers you’d like to fix, but… this may give you a start:

    import re
    retelephone = re.compile(r'[-\(\)\s]') # remove dashes, open/close parens, and spaces
    for empid, row in employees.iteritems():
        retelephone.sub('',row['telephone'])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large collection of data in an excel file (and csv files).
I have an Excel Spreadsheet like this id | data for id | more
I have a web page where it will input an excel/CSV file from the
I have 12 .csv files produced by another program. The .csv files contain numeric
I'm doing updates from Excel/CSV files to a database. I'm using LINQ to SQL
How do I save each sheet in an Excel workbook to separate CSV files
I am working on an Cocoa project using SQL databases, or CSV/Excel files as
I have Excel add-in which I add so many class modules that it is
We have an Excel 2002/XP based application that interacts with SQL 2000/5 to process
I have an excel spreadsheet in a format similar to the following... | NAME

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.