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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:28:53+00:00 2026-06-14T22:28:53+00:00

What I want to do is simple: collection = {‘a’:[], ‘b’:[], ‘c’:[]} values =

  • 0

What I want to do is simple:

collection = {'a':[], 'b':[], 'c':[]}
values = [1,2,3]

I want to make a function that produces the following: (append the values into the list element of the dictionary, the dic and the list are the same length)

{'a':[1], 'b':[2], 'c':[3]}

This is simple enough and I can do it using, a couple of for x in. But I want to do this in one line. (using two loops in the same line) and I can not get the syntax to work.

I have tried some things similar to this, but they all result in syntax error:

collection[c].append(value), for c in d.iteritems(), for value in values
  • 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-14T22:28:54+00:00Added an answer on June 14, 2026 at 10:28 pm

    You can’t do what you want to do on one line. You can create a new dictionary on one line though:

    {k: collection[k] + [v] for k, v in zip(collection.keys(), values)}
    

    Result:

    >>> collection = {'a':[], 'b':[], 'c':[]}
    >>> values = [1,2,3]
    >>> {k: collection[k] + [v] for k, v in zip(collection.keys(), values)}
    {'a': [1], 'c': [2], 'b': [3]}
    

    This is called a dict comprehension. Like a list comprehension and a generator expression, you can use multiple loops in one of those, but you don’t need one here. zip() will pair up the keys from collection with the integers from values.

    To modify a dict in-place, you’ll have to use 2 lines at least:

    for k, v in zip(collection.keys(), values):
        collection[k].append(v)
    

    Python does accept that on one line, but that goes against just about every styleguide I can look up for you:

    for k, v in zip(collection.keys(), values): collection[k].append(v)
    

    Python throws a syntax error because it interprets your line as a tuple of expressions (the commas make it a tuple), and two of your expressions are for statements, which cannot be used in an expression.

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

Sidebar

Related Questions

I want to use List.Find() on a simple collection that does not implement Find().
I want to have a simple select->option dropdown list that I am not passing
For my NodeJS project, I want to make a simple module that will give
I want to make a simple image viewing program, that just takes the images
I have a very simple SQL table that I want to import into Solr
I'm making a simple program that maintains a list of numbers, and I want
Really simple problem: I want to split a connection string into its keyword /
I want to display a simple list of languages. class Language extends Backbone.Model defaults:
I want to load json data in form of a tree into Backbone Collection.
I want a simple unit test configuration. My understanding is that the sqlite fluent

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.