Random filter a python list of dictionary to get a new list of dictionary based on same key name

Question:

I want to form a new list of dictionaries by random choosing dictionary from existing list of dictionary based on same key name.

existing_list = [{'topic1': 'question1'}, {'topic2': 'question2'}, {'topic3': 'question3'}, {'topic2': 'question4'}, {'topic2': 'question5'}, {'topic1': 'question2'}, {'topic1': 'question3'}, {'topic3': 'question5'}, {'topic3': 'question6'}]

The new list should choose two random dictionary having same key name:

 new_list = [{'topic1': 'question1'}, {'topic1': 'question3'}, {'topic2': 'question2'}, {'topic2': 'question5'}, {'topic3': 'question5'}, {'topic3': 'question3'}]

Taking suggestions below i change the data structures:

arrange = {}
for item in questions:
    arrange.setdefault(item['parent'], []).append(item['question_link'])

    question_lists_of_lists = [random.sample(arrange[topic], 2) for topic in arrange]
    question_lists= sum(question_lists_of_lists,[])

Now i am looking for better alternative to achieve the new list using:

new_list = []
for k, v in arrange.items():
    for i in range(2):
        random_value = random.choice(v)
        new_list.append({k: random_value})
        arrange[k].remove(random_value)
Asked By: prem

||

Answers:

i think your main problem is how you store your data. having a list of dictionaries with a single key-value pair is not that easy to work with.

a better approach might be to store the different questions in a list for each topic so kind of like this:

d = {'topic1': [question1, question2, question3], 'topic2': [question1, question2, question3]}

then you can iterate through the dictionary like so:

for topic, questions in d.items():
    q1 = d[topic][random(0, len(questions)].pop()
    q2 = d[topic][random(0, len(questions)].pop()

then you can store those questions in some list or use them however you want.

(btw the list.pop() method actually removes the element from list but since ‘questions’ is a local variable in the loop it should be fine i think)

Answered By: maxxel_

I’m pretty sure this is the code that you are looking for:

import random
existing_list = [{'topic1': 'question1'}, {'topic2': 'question2'}, {'topic3': 'question3'}, {'topic2': 'question4'}, {'topic2': 'question5'}, {'topic1': 'question2'}, {'topic1': 'question3'}, {'topic3': 'question5'}, {'topic3': 'question6'}]
new_list = []
d = {}  # creating a new dictionary for better storage overview

# adding key-value pairs into the dictionary
for element in existing_list:
    for k, v in element.items():
        if k in d.keys():
            d[k].append(v)
        else:
            d[k] = [v,]

# randomly choosing values from the dictionary
for k, v in d.items():
    for i in range(2):
        random_value = random.choice(v)
        new_list.append({k: random_value})
        d[k].remove(random_value)

print(new_list)
Answered By: BokiX
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.