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

  • SEARCH
  • Home
  • 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 8286637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:49:50+00:00 2026-06-08T11:49:50+00:00

I have a dict that describes a mapping I want applied to every row

  • 0

I have a dict that describes a mapping I want applied to every row in a CSV file.

dict1 = {"key1":["value1", "value2"], "key2":["value3"]}

My program should read one row and map the key in a specific column to the value(s) provided by the dict. If there’s only one value per key, then the script should write to a new file the row containing the new value. If there are multiple values to a key, then there should be one new row written per value.

For example, csvin contains 2 rows. One row has a column in which key1 is present, and the other has key2. In this case, the output file csvout should contain more rows than csvin, in effect 3. Two of the rows (associated with key1) will be identical except for one single value.

My current script is this:

def convSan(sfin, cfout):
    with open(sfin, "rb") as fin:
        with open(cfout, "wb") as fout:
            csvin = csv.reader(fin)
            csvout = csv.writer(fout, delimiter=",")
            fline = csvin.next()
            csvout.writerow(fline)

        for row in csvin:
            row[25] = dict1[row[25]]
            csvout.writerow(row)

This produces an output file with the same number of columns as the input file, but populates every field with the correct new values (some fields are now lists of values).

The answer provided by @sr2222 works in the case of simple lists, but I cannot get it to work in my particular case.

Help is appreciated.

  • 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-06-08T11:49:51+00:00Added an answer on June 8, 2026 at 11:49 am

    First:

    for index, value in enumerate(list1):
        list1[index] = list2[index]
    

    Is a cleaner way to format your first loop. However, that is equivalent to list1 = copy.copy(list2). I think what you are trying to do is:

    normalized_values = ['123', '456']
    content = ['a123', '123', 'b456', '789']
    for index, value in enumerate(content):
        for normalized_value in normalized_values:
            if normalized_value in value:
                content[index] = normalized_value
    

    Which will leave you with:

    content = ['123', '123', '456', '789']
    

    Edit after question update:

    replacement_map = {'123' : ('a123', '1234'), '456' : ('00456',)}
    input = ['123', '456', '234', '123', '789']
    output = []
    for value in input:
        try:
            output.extend(replacement_map[value])
        except KeyError:
            output.append(value)
    

    The try/except is equivalent to:

    if value in replacement_map:
        output.extend(replacement_map[value])
    else:
        output.append(value)
    

    In response to comment on building the map from 2 lists as described above (note this will only behave correctly if you can always assume list1 and list2 are the same length):

    replacement_map = {}
    for key, value in zip(list1, list2):
        try:
            replacement_map[key].append(value)
        except KeyError:
            replacement_map[key] = [value]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dict that I want to convert in JSON using simplejson. How
I have a dict that has many elements, I want to write a function
I have a string that looks like this: Name1=Value1;Name2=Value2;Name3=Value3 Is there a built-in class/function
My issue is as follows. We have dict that has all keys and values
I have a rowset that looks like this: defaultdict(<type 'dict'>, { u'row1': {u'column1': 33,
I have a plist that looks like this: <dict> <key>Aaron</key> <dict> <key>number</key> <string>1234</string> <key>country</key>
Let's say that I have class , that uses some functionality of dict .
I have a list of dicts and I want to compare each dict in
I have a dict that looks like this { keyword1:3 , keyword2:1 , keyword3:5
I have a very large defaultdict(dict) that looks something like this: data['w']['x']['y']['z']={'a':5,'b':10} I'm trying

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.