Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing list and dictionary type parameter with Python

When I run this code

def func(x, y, *w, **z):
  print x
  print y
  if w:
      print w

  if z:
      print z
  else:
      print "None"

func(10,20, 1,2,3,{'k':'a'})

I get the result as follows.

10
20
(1, 2, 3, {'k': 'a'})
None

But, I expected as follows, I mean the list parameters (1,2,3) matching *w, and dictionary matching **z.

10
20
(1,2,3)
{'k':'a'}

Q : What went wrong? How can I pass the list and dictionary as parameters?

Added

func(10,20, 10,20,30, k='a')

seems to be working

like image 274
prosseek Avatar asked Jan 19 '26 01:01

prosseek


1 Answers

Put two asterisks before the dictionary:

func(10,20, 1,2,3,**{'k':'a'})
like image 69
Daniel Stutzbach Avatar answered Jan 20 '26 17:01

Daniel Stutzbach



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!