Newbie: How to pass a dictionary to a function?

B

BonusOnus

How do I pass a dictionary to a function as an argument?


# Say I have a function foo...
def foo (arg=[]):
x = arg['name']
y = arg['len']

s = len (x)

t = s + y

return (s, t)

# The dictionary:

dict = {}
dict['name'] = 'Joe Shmoe'
dict['len'] = 44


# I try to pass the dictionary as an argument to a
# function

len, string = foo (dict)

# This bombs with 'TypeError: unpack non-sequence'

What am I doing wrong with the dictionary?
 
R

Ricky Zhou

How do I pass a dictionary to a function as an argument?


# Say I have a function foo...
def foo (arg=[]):
Try:

def foo(arg={}):

Thanks,
Ricky

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQFH+vAAZBKKLMyvSE4RAoEhAJ9CdUt/hXIY1npXuo9yNQUdbF5IggCeNsqe
61q8Gamq/DvhOR+wEEfwVxs=
=RJQJ
-----END PGP SIGNATURE-----
 
M

Matt Nordhoff

BonusOnus said:
How do I pass a dictionary to a function as an argument?


# Say I have a function foo...
def foo (arg=[]):
x = arg['name']
y = arg['len']

s = len (x)

t = s + y

return (s, t)

I assume you actually indented the body of the function?
# The dictionary:

dict = {}
dict['name'] = 'Joe Shmoe'
dict['len'] = 44

'dict' is the name of a built-in type. You should name your variable
something else.
# I try to pass the dictionary as an argument to a
# function

len, string = foo (dict)

'len' is the name of a built-in function and 'string' is a module in the
standard library. You should name both of them something else.
# This bombs with 'TypeError: unpack non-sequence'

What am I doing wrong with the dictionary?

It would be helpful to provide the full traceback, since that says what
line the problem is on...

HTH (it probably won't)
--
 
D

Dan Bishop

How do I pass a dictionary to a function as an argument?

The same way you pass any other argument.
# Say I have a function foo...
def foo (arg=[]):

It's generally a bad idea to use [] as a default argument.
x = arg['name']
y = arg['len']

s = len (x)

t = s + y

return (s, t)

Apart from the lack of indentation (and meaningless variable names),
it looks correct.
# The dictionary:

dict = {}
dict['name'] = 'Joe Shmoe'
dict['len'] = 44

"dict" is a built-in name. Don't redefine it.
# I try to pass the dictionary as an argument to a
# function

len, string = foo (dict)

Don't redefine "len" or "string" either.
# This bombs with 'TypeError: unpack non-sequence'

What am I doing wrong with the dictionary?

Runs fine for me as soon as I fixed the indentation.
 
J

Jason Scheirer

How do I pass a dictionary to a function as an argument?

# Say I have a function foo...
def foo (arg=[]):
x = arg['name']
y = arg['len']

s = len (x)

t = s + y

return (s, t)

# The dictionary:

dict = {}
dict['name'] = 'Joe Shmoe'
dict['len'] = 44

# I try to pass the dictionary as an argument to a
# function

len, string = foo (dict)

# This bombs with 'TypeError: unpack non-sequence'

What am I doing wrong with the dictionary?

You want to
return s, t
NOT return (s, t) -- this implicitly only returns ONE item
 
G

Gabriel Genellina

How do I pass a dictionary to a function as an argument?

The indentation is lost, so it's not easy to check your program.
# Say I have a function foo...

Original: def foo(arg=[]). An empty list isn't a good default value here,
perhaps you intended to use {}? Anyway, don't use mutable default values;
see this FAQ entry:
http://www.python.org/doc/faq/general/#why-are-default-values-shared-between-objects

def foo(arg):
x = arg['name']
y = arg['len']
s = len(x)
t = s + y
return s, t

# don't use dict as a variable name, you're hiding the builtin dict type
my_dict = {}
my_dict['name'] = 'Joe Shmoe'
my_dict['len'] = 44

# don't use len as a name either
# nor string!
length, string = foo(my_dict)
# This bombs with 'TypeError: unpack non-sequence'
What am I doing wrong with the dictionary?

Nothing. Written as above, it works fine. Don't retype your programs,
always copy & paste from the same source you're executing. If you get an
exception, post the entire traceback with the exact exception name and
message.
 
G

Gabriel Genellina

En Tue, 08 Apr 2008 01:57:47 -0300, Jason Scheirer
You want to
return s, t
NOT return (s, t) -- this implicitly only returns ONE item

To avoid confusing the poor newbie: No, they're absolutely the same thing,
in both cases you're returning a tuple with two items:

py> def f():
.... return 1, 2
....
py> def g():
.... return (1, 2)
....
py> f()
(1, 2)
py> g()
(1, 2)
py> a, b = f()
py> c, d = g()
py> c
1
py> d
2

When one says that a function like those above returns "two things",
actually it means that the function returns "a tuple with two elements",
not two separate objects.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,069
Latest member
SimplyleanKetoReviews

Latest Threads

Top