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 7557513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:09:06+00:00 2026-05-30T12:09:06+00:00

How do I build a dict using list comprehension? I have two lists. series

  • 0

How do I build a dict using list comprehension?

I have two lists.

 series = [1,2,3,4,5]
 categories = ['A', 'B', 'A', 'C','B']

I want to build a dict where the categories are the keys.

Thanks for your answers I’m looking to produce:

 {'A' : [1, 3], 'B' : [2, 5], 'C' : [4]}

Because the keys can’t exist twice

  • 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-30T12:09:07+00:00Added an answer on May 30, 2026 at 12:09 pm

    You have to have a list of tuples. The tuples are key/value pairs. You don’t need a comprehension in this case, just zip:

    dict(zip(categories, series))
    

    Produces {'A': 3, 'B': 5, 'C': 4} (as pointed out by comments)

    Edit: After looking at the keys, note that you can’t have duplicate keys in a dictionary. So without further clarifying what you want, I’m not sure what solution you’re looking for.

    Edit: To get what you want, it’s probably easiest to just do a for loop with either setdefault or a defaultdict.

    categoriesMap = {}
    for k, v in zip(categories, series):
        categoriesMap.setdefault(k, []).append(v)
    

    That should produce {'A': [1, 3], 'B': [2, 5], 'C': [3]}

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to dynamically build a list of admin actions using the get_actions()
I am trying to build a dictionary with two keys but am getting a
I want to execute a mapreduce query, in erlang, that contains two map phases
I have found here on stackoverflow a method to extend django's built-in authentication using
In my code there's numerous comparisons for equality of various containers (list, dict, etc.).
As suggested in answers to a previous question , I tried using Erlang proplist
i'm writing an installer using py2exe which needs to run in admin to have
What is the most pythonic way to build a dictionary where I have the
I am using django 1.1.4 and python 2.6.6 with mod_wsgi to build a website
I have an output (a list) of items, like such: Root Branch1 LeafA LeafB

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.