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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:55:44+00:00 2026-05-23T13:55:44+00:00

Novice question here. I’m working on finding every single combination of various string elements,

  • 0

Novice question here.

I’m working on finding every single combination of various string elements, and then want to place them into a list.

import itertools 

mydata = [ ]

main = [["car insurance", "auto insurance"], ["insurance"], ["cheap", "budget"],
      ["low cost"], ["quote", "quotes"], ["rate", "rates"], ["comparison"]]

def twofunc(one, two):
    for a, b in itertools.product(main[one], main[two]):
        print a, b

def threefunc(one, two, three):
    for a, b, c in itertools.product(main[one], main[two], main[three]):
        print a, b, c

twofunc(2, 0)  #extremely inefficient to just run these functions over and over. alternative?
twofunc(3, 0)
twofunc(0, 4)
twofunc(0, 5)
twofunc(0, 6)
threefunc(2, 0, 4)
threefunc(3, 0, 4)
threefunc(2, 0, 5)
threefunc(3, 0, 5)
threefunc(2, 0, 6)
threefunc(3, 0, 6)

The above code just prints out each permutation, but doesn’t append the values to the list. I’ve tried various variations on the append method and still have no luck.

Can anyone help me with placing these values into the mydata list. I assume that each string will have to be a seperate list, so it will end up being a list of lists. It should look like the following, but I’ll also need some way to include “tags” in the future, or just values for if the string contains a value.

[["cheap car insurance"],
["cheap auto insurance"],
["budget car insurance"],
["budget auto insurance"],
["low cost car insurance"],
["low cost auto insurance"],
...

so, eventually, will end up: 1 means that the strings contains the word car/cheap, and 0 means that it doesn’t. The reason I mention this is just to inquire if a list is the proper data structure for this task.

                         car        cheap 
cheap car insurance       1            1
cheap auto insurance      0            1 
budget car insurance      1            0
budget auto insurance     0            0

Can anyone help.

I’ve completed this task in R, which is pretty amenable to this task, and just wanted to reproduce it in Python.

  • 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-23T13:55:45+00:00Added an answer on May 23, 2026 at 1:55 pm

    To get the return values of twofunc and threefunc into a list, you can change the return statements so that lists are returned. Then append the result to mydata. An example follows for twofunc:

    def twofunc(one, two):
        for a, b in itertools.product(main[one], main[two]):
            return [a, b]
    
    mydata.append(twofunc(2,0))
    

    That said, I’m not familiar with R, so obviously don’t know what data structure you might be using there. For your stated goal, keeping this in a list might get complicated. However, creating a simple class to do this shouldn’t be too difficult. You could then have a list made up of instantiations of this class.

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

Sidebar

Related Questions

This could be a novice question, but here goes... Why do we use data**.d**
I am a AS3 novice learning PureMVC and want to write code following best
I'm a Python novice, trying to use pyCurl. The project I am working on
This may be a novice question, but I can't figure it out by inspecting
I have a very novice question, so apologies if the answer to this is
I recently read a question on here about static and dynamic linking, which reminded
Searching here I found that this question was already asked , but I think
I'm a novice, both here at stack overflow as well as on sql server,
This is almost certainly a very novice question, but being as I am a
This may be a lame question but I am a total novice with regular

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.