my dictionary looks like:
account is: {'Discover': ['username', 'dddddd']}
I've tried:
print(account[0])
but it doesn't work.
Im trying to achieve printing out:
Discover
If someone could help me out? Its been a little while.
Thank you
my dictionary looks like:
account is: {'Discover': ['username', 'dddddd']}
I've tried:
print(account[0])
but it doesn't work.
Im trying to achieve printing out:
Discover
If someone could help me out? Its been a little while.
Thank you
Since Discover
is the only key you could do print account.keys()[0]
.
But if you ever had more keys this won't work every time since dictionaries are arbitrarily ordered (in other words, have no sense of order), you will need to iterate over the keys()
list, or simply print account.keys()
to print the whole keys list.
Discover
is a key of your dictionary. You can access the keys collection in your dictionary using keys() method. Then use each key to access the associated element.
for key in account.keys():
print(key)
print(account[key])
It sounds like you want to see the keys of your dict (which in this example is just a single key, 'Discover'
). Given a dict key, you can use that key to index into the dict to get the associated value. Here are some examples using the values you gave:
>>> account = {'Discover': ['username', 'dddddd']}
>>> account
{'Discover': ['username', 'dddddd']}
>>> account.keys()
['Discover']
>>> account['Discover']
['username', 'dddddd']
>>>