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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:17:20+00:00 2026-05-14T15:17:20+00:00

In Perl, I can do this: push(@{$h->[x]}, y); Can I simplify the following python

  • 0

In Perl, I can do this:

push(@{$h->[x]}, y);

Can I simplify the following python codes according to above Perl example?

if x not in h:
  h[x] = []
h[x].append(y)

I want to simplify this, because it goes many places in my code, (and I cannot initialize all possible x with []). I do not want to make it a function, because there is no ‘inline’ keyword.

Any ideas?

  • 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-14T15:17:20+00:00Added an answer on May 14, 2026 at 3:17 pm

    A very elegant way (since Python 2.5) is to use defaultdict from the “collections” module:

    >>> from collections import defaultdict
    >>> h = defaultdict(list)
    >>> h['a'].append('b')
    >>> h
    defaultdict(<type 'list'>, {'a': ['b']})
    

    defaultdict is like a dict, but provides a default value using whichever constructor you passed to it when you created it (in this example, a list).

    I particularly like this over the setdefault dict method, because 1) you define the variable as a defaultdict, and generally no other changes are required on the code (except perhaps to remove previous kludges for default values); and 2) setdefault is a terrible name 😛

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

Sidebar

Related Questions

I tried this example in Perl. Can someone explain why is it true? if
can anybody explain me this print statement in the following perl program. #! /usr/bin/perl
I'm learning Tcl. In Perl I can do this: $ perl -e 'for ($i
I know this is possible in Perl, but I was wondering if this can
How can I do this Perl code in PHP? print unpack (H*, pack (B*,
Can someone explain this to me. I am pretty good with perl regular expressions
The Term::Size-module jumbles up the encoding. How can I fix this? #!/usr/bin/env perl use
How can I run my perl CGI script without apache? This is for testing
I've seen this one-liner perl -lane '$_{$F[0]}+=$F[1]}print$_ $_{$_}for keys%_;{' file here: How can I
I am new to perl, and can't seem to find answers about this anywhere.

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.