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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:55:21+00:00 2026-06-06T14:55:21+00:00

Currently, I’m trying to fill a dictionary in Python but I think what I’m

  • 0

Currently, I’m trying to fill a dictionary in Python but I think what I’m doing is a little bit redundant. Is there a more pythonic way to do the following:

if not pattern_file_map.get(which_match):
    pattern_file_map[which_match] = [line]
else:
    pattern_file_map[which_match].append(line)

where pattern_file_map is a dictionary.

I know that there is a certain idiom to use when checking if there is a key in a dictionary, like
this question, but I I just want to fill this dictionary with a lists.

  • 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-06T14:55:21+00:00Added an answer on June 6, 2026 at 2:55 pm

    You could use

    pattern_file_map.setdefault(which_match, []).append(line)
    

    instead.

    Other people might suggest using a collections.defaultdict(list) instead, which is another option, but be warned that this might hide errors, since it will silently create all keys you access.

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

Sidebar

Related Questions

currently I'm developing an app for WP7 but came across a little problem with
currently, I`m implementing a map App with Mono4Droid and there I`m using a WebView
Currently working with converting SQLException error messages into messages that are more useful for
Currently I am trying to use a bunch of custom perl modules, test.pm as
Currently playing about with KnockoutJS. Just trying to update an observable array from a
Currently It just opens up the xml, but i want to get a dialog
Currently I'm learning C++ and I've been trying to create a simple image processing
Currently I’m trying to get the hang of a block copy with my current
Currently I'm doing some unit tests which are executed from bash. Unit tests are
Currently, I am writing up a bit of a product-based CMS as my first

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.