How to get all keys with the same values?
📁 словарь, python
There is a dictionary: mydict = {'main':'open','second':'close','third':'open'}. I need to get all keys with the value 'open' from it.
I tried this code:
mydict = {'main':'open','second':'close','third':'open'}print(list(mydict.keys())[list(mydict.values()).index('open')])It only outputs main. How to output all keys with the value 'open'?
Your error is that in the last statement you are truncating the resulting string, assuming that the numbers it contains are one character long. But that is naturally not the case. To avoid this error, you can, for example, use lists:
v = []for i in range(1, n+1): v += [str(i)] * iprint("" "".join(v[:n]))Using list comprehensions, this can be written in one line:
print("" "".join([str(i) for Log in to leave an answer