Python Forum

Full Version: functions and dictionary
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,
I am using functions, and I am passing the argument as dictionary, is there a way to get the value of the dictionary? I am able to get the keys though. I am interested to get the values from the dictionary.

def odd_even(sequence):
    for x in sequence:
        print(x)

odd_even({
    "store1": ["suman"],
    "store2": ("su")
})
Output:
store1 store2
Your question doesn't really have anything to do with functions. Do you know how to access values in a dictionary by key? If you just want all the values, regardless of the key, of course there's a way to do that. See the docs to find the relevant method ;).
One can use tools built-in into interactive interpreter. Like: hmm, what methods can I apply to dictionaries?

>>> dict.    # two times TAB
dict.clear(      dict.get(        dict.mro(        dict.setdefault(
dict.copy(       dict.items(      dict.pop(        dict.update(
dict.fromkeys(   dict.keys(       dict.popitem(    dict.values(
There are promising names like items, keys and values. Let's check what values are about:

>>> help(dict.values)
Help on method_descriptor:

values(...)
    D.values() -> an object providing a view on D's values
(END)   # press Q-key to exit help
Change line 2 to
for key, value in sequence:

Then print keys, values, both, whatever