Answer the question
In order to leave comments, you need to log in
Derive random key from json dictionary?
The essence of the question is how to derive a random key from a json dictionary.
Googled everything that is possible, some crutches and a code of 10 lines, and that does not work. Isn't there an easy way.
def randkey():
with open("file.json") as file:
dict = json.load(file)
for k, v in sorted(dict.items())[-1:]:
randkeyvalue = f"Имя: {v['Name']}\n" \
f"Возраст: {v['Age']}\n"
Answer the question
In order to leave comments, you need to log in
Derive random key from json dictionary
Derive random key from json dictionary
from random import choice
choice(list(dct.keys()))
import random
def randitem():
with open("file.json") as file:
dict = json.load(file)
dictlist = list(dict.items())
random.shuffle(dictlist)
for k, v in dictlist[-1:]:
randkeyvalue = f"Имя: {v['Name']}\n" \
f"Возраст: {v['Age']}\n"
dictlist = random.shuffle(list(dict.items()))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question