Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to convert a nested dictionary to a list, combine it with existing list and display results

#nested dictionary

card_values = {
"normal": [2, 3, 4, 5, 6, 7, 8, 9, 10], 
"suited": {"J":10, "Q":10, "K":10, "A":11}
}

#code I wrote to try and iterate over the values

all_cards = []
for i in card_values:
    for j in card_values[i]:
        if j == "J" or j == "Q" or j == "K":
            all_cards.append(10)
        elif j =="A":
            all_cards.append(11)
        else:
            all_cards.append(j)
print(all_cards)

without doing this, can we call the value corresponding to the key inside the nested dictionary in a for loop?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#output needed

all_cards = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

>Solution :

I would make a set to get the unique values and then sort it.

card_values = {
    "normal": [2, 3, 4, 5, 6, 7, 8, 9, 10],
    "suited": {"J":10, "Q":10, "K":10, "A":11}
}

all_cards = sorted(
    set(card_values["normal"])
    | set(card_values["suited"].values())
)

print(all_cards)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading