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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:31:24+00:00 2026-06-01T08:31:24+00:00

I am working with dictionaries in python. s = {‘k1’:[‘aa’, ‘bb’, ‘cc’, ‘dd’, ‘ee’,

  • 0

I am working with dictionaries in python.

s = {'k1':['aa', 'bb', 'cc', 'dd', 'ee', 'ff', 'gg']}

I have created code as :

d = {}
for values in enumerate(s.values()[0]):
    if values[0]<2:
        d[values[1]] = 'True'
    else:
        d[values[1]] = 'False'

Can I convert this into one liner code using list comprehension or lambda?

I need answer as :

{'aa': 'True', 'bb': 'True', 'cc': 'False', 'dd': 'False', 'ee': 'False', 'ff': 'False', 'gg': 'False'}

updated – sry code typing mistake

  • 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-01T08:31:25+00:00Added an answer on June 1, 2026 at 8:31 am

    In response to:

    d = {}
    for values in enumerate(s.values()[0]):
        if values[0]<2:
            d['ans'] = 'True'
        else:
            d['ans'] = 'False'
    

    the one liner is:

    d = {'ans': str(len(s.values()[0]) < 2)}
    

    What you are doing is nonsensical. Essentially the code you posted will always set d['ans'] to 'True' when s.values()[0] has a length less than 2 (else 'False'). This is because the first element in the 2-tuple yielded by enumerate is the index (which you are comparing with <2). For all cases where the length of list is greater than 2, the else clause will keep setting it to 'False'. You dont even need a loop for this.

    Update:
    For your new version:

    d = dict( [(val, str(index < 2)) for (index, val) in enumerate(s.values()[0])] )
    

    or

    d = dict( [(i , "True") for i in li[:2]] + [(i, "False") for i in li[2:]] )
    

    update 2: or if you want to update an existing dictionary:

    d.update( (( (val, str(index < 2)) for (index, val) in enumerate(s.values()[0]) )) )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two python dictionaries containing information about japanese words and characters: vocabDic :
I know how python dictionaries store key: value tuples. In the project I'm working
Working on joining up two legacy DB systems into new database where I can
I usually use the following idiom when working with a Python dictionary: try: val
EDIT: The correct version of the code which works can be found at :
I am working on a personal project where I need to manipulate values in
I am working on a Google app engine project where I have saved some
I'm working through exercises in Building Skills in Python , which to my knowledge
Working with a Lucene index, I have a standard document format that looks something
The Python native capabilities for lists, sets & dictionaries totally rock. Is there a

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.