Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
functions and dictionary
#1
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
Reply
#2
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 ;).
Reply
#3
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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
Change line 2 to
for key, value in sequence:

Then print keys, values, both, whatever
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  functions in a dictionary Skaperen 7 3,466 Apr-27-2019, 02:25 AM
Last Post: rxndy

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020