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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:55:25+00:00 2026-06-04T21:55:25+00:00

Just a little background: I’m making a program where a user inputs a skeleton

  • 0

Just a little background: I’m making a program where a user inputs a skeleton text, two numbers (lower and upper limit), and a list of words. The outputs are a series of modifications on the skeleton text.

Sample inputs:

text = "Player # likes @." (replace # with inputted integers and @ with words in list)
lower = 1
upper = 3
list = "apples, bananas, oranges"

The user can choose to iterate over numbers first:

Player 1 likes apples.
Player 2 likes apples.
Player 3 likes apples.

Or words first:

Player 1 likes apples.
Player 1 likes bananas.
Player 1 likes oranges.

I chose to split these two methods of outputs by creating a different type of dictionary based on either number keys (integers inputted by the user) or word keys (from words in the inputted list) and then later iterating over the values in the dictionary.

Here are the two types of dictionary creation:

def numkey(dict): # {1: ['Player 1 likes apples', 'Player 1 likes...' ] }

    text, lower, upper, list = input_sort(dict)
    d = {}

    for num in range(lower,upper+1):
        l = []
        for i in list:
            l.append(text.replace('#', str(num)).replace('@', i))
        d[num] = l
    return d

def wordkey(dict): # {'apples': ['Player 1 likes apples', 'Player 2 likes apples'..] }

    text, lower, upper, list = input_sort(dict)
    d = {}

    for i in list:
        l = []
        for num in range(lower,upper+1):
            l.append(text.replace('#', str(num)).replace('@', i))
        d[i] = l
    return d

It’s fine that I have two separate functions for creating different types of dictionaries but I see a lot of repetition between the two. Is there any way I could make one dictionary function and pass in different values to it that would change the order of the nested for loops to create the specific {key : value} pairs I’m looking for?

I’m not sure how this would be done. Is there anything related to functional programming or other paradigms that might help with this? The question is a little abstract and more stylistic/design-oriented than anything.

  • 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-04T21:55:26+00:00Added an answer on June 4, 2026 at 9:55 pm

    You don’t need a dictionary to generate the output. You could use something like:

    import itertools
    
    numbers = range(lower, upper + 1)
    words = "a, b, c".split(", ")
    
    data = (numbers, words) if numbers_first else (words, numbers)
    for n, w in itertools.product(*data):
        if not numbers_first: n, w = w, n
        print("Player %d likes %s." % (n, w))
    

    To avoid the if inside the loop you could generate the format string dynamically e.g.:

    template = "Player # likes @."
    subs = ("{n}", "{w}") if numbers_first else ("{w}", "{n}")
    format = make_format(template, subs) # escape {}, replace # and @
    
    # ...
    for n, w in product(*data):
        print(format.format(n=n, w=w))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just a little background on my project: I'm implementing an SMS encryption program using
A little background so you understand what I'm trying to do. I am making
A little background info; I'm making an iOS App that uses facebook integration. I
For a little background information, I am trying to sync two calendars (one of
Just a little background: I am using Access 2010 to create forms and VBA
A little background: I am trying to create a lightweight cookieless database-backed user session
Just a little background, our marketing department has been using static weather images on
Just a little confused here... I have a function in postgres, and when I'm
This sounds funny..just a little experiment. i wanted to simulate a drag drop of
I am sure it's just a little mistake, but i can't find it. When

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.