0

I am working on some sentence formation like this:

sentence = "PERSON is ADJECTIVE"
dictionary = {"PERSON": ["Alice", "Bob", "Carol"], "ADJECTIVE": ["cute", "intelligent"]}

I would now need all possible combinations to form this sentence from the dictionary, like:

Alice is cute
Alice is intelligent
Bob is cute
Bob is intelligent
Carol is cute
Carol is intelligent

Can we also make this scale up for longer sentences?

Example:

sentence = PERSON is ADJECTIVE and is from COUNTRY 
dictionary = {"PERSON": ["Alice", "Bob", "Carol"], "ADJECTIVE": ["cute", "intelligent"], "COUNTRY": ["USA", "Japan", "China", "India"]}

This again provides all possible combinations

2
  • Use two nested loops. Commented Dec 12, 2021 at 15:28
  • @Shubhashish , I am a newbie, could you please post a logic, this would also scale up, any capital letters would have a list of values in the dictionary to fetch from Commented Dec 12, 2021 at 15:33

2 Answers 2

2

You can do this,

dictionary = {"PERSON": ["Alice", "Bob", "Carol"], "ADJECTIVE": ["cute", "intelligent"]}

for i in dictionary["PERSON"]:
    for j in dictionary["ADJECTIVE"]:
        print(f"{i} is {j}")

Output:

Alice is cute
Alice is intelligent
Bob is cute
Bob is intelligent
Carol is cute
Carol is intelligent

Here in the outer loop iterate through each of the elements of the list having key PERSON and the inner loop iterate through the element of the list having key ADJECTIVE. being inside the outer loop the inner loop repeat itself for each element of the first list/list with key PERSON.

Edit regarding your question update:

You can use another loop inside the inner loop, like this.

dictionary = {"PERSON": ["Alice", "Bob", "Carol"], "ADJECTIVE": ["cute", "intelligent"], "COUNTRY": ["USA", "Japan", "China", "India"]}

for PERSON in dictionary["PERSON"]:
    for ADJECTIVE in dictionary["ADJECTIVE"]:
        for COUNTRY in dictionary["COUNTRY"]:
            sentence = f"{PERSON} is {ADJECTIVE} and is from {COUNTRY}"
            print(sentence)
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, this is useful, but do we have any other way where we can scale up the same for longer sentences? Example: sentence = PERSON is ADJECTIVE and is from COUNTRY and dictionary = {"PERSON": ["Alice", "Bob", "Carol"], "ADJECTIVE": ["cute", "intelligent"], "COUNTRY": ["USA", "Japan", "China", India]}. This should essentially provide all possible combinations from the dictionary.
@BalajiSrinivasan This should be asked in a new question with the focus on what you try to and some more details. Actually question do not deal with that fact.
@BalajiSrinivasan use another loop for COUNTRY is side the inner loop/second loop, like this, for k in dictionary["COUNTRY"]: print(f"{i} is {j} and is from {k}")
0
for x in sentence:
  for y in dictionary:
    print(x, y)

something like this?

1 Comment

Please improve your answer it looks more like a comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.