How do I create a list with two indexes: The key and the appendant value of a dictionary. The key and the value should be random chosen out of the dictionary. I've already tried it with (these are my Latin words):
d = {}
d['house'] = "domus, domus"
da1 = d.items()
da = random.sample(wa1,1)
print (da)
When I do it like that, I get a list with only one index: The key AND the value. But I need a list with two indexes for being able to do:
dav = da[0]
dal = da[1]
def solution():
dal = da[1]
lbl.configure(text=dal)
def continue():
dav = da[0]
lbl.configure(text=dav)
lbl = Label(window,text=dav)
lbl.pack()
btnl = Button(window,text="Show solution",command=solution)
btnl.pack()
btnw = Button(window,text="Continue",command=continue)
btnw.pack()
Is there anyone who can help me?
You are getting back a list which contains a tuple, which itself has two elements. For example:
da == [('house', 'domus, domus')]
You can index into that by using two indices:
>>> da[0][0]
'house'
>>> da[0][1]
'domus, domus'
If you want to make da
into a list with two elements, you can do:
da = [x for x in da[0]]
Alternatively, you can just issue
da = random.sample(d.items(),1)[0]
to get a tuple with two elements
('house', 'domus, domus')
which you can make into a list with list(da)
if that's required.