Python Forum
calling a function which takes a dictionary as argument
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
calling a function which takes a dictionary as argument
#1
Hi all,

I'm having trouble with calling a function which takes a dictionary as argument
for example If I have

dic = {3:0.2, 4:0.5, 5:0.1, 6:0.8, 7:0.34}

def function(**dic):
   if len(dic) == 0:
      return dic
   elif len(dic)>0:
      dic [100] = 0.1
      return dic

function(**dic)
1. basically I first have a dictionary defined, 
2. in the function(which takes this dictionary as argument), if the length of this dictionary is 0, return the original dictionary(an empty dictionary), if the length is larger than zero, add one element to the dictionary, and return the updated version of the dictionary.

so if I want to call this function and get the returned dictionary,
function(**dic)   doesn't work,  what should I do instead? or what I did wrong
Reply
#2
You don't need to use the **kwarg syntax to just pass a dict to a function.
**kwarg is used for passing a number of keyword arguments. Any keyword arguments the signature doesn't explicitly handle are put in the kwarg dict doing this.

anyway... this is all you need:
dic = {3:0.2, 4:0.5, 5:0.1, 6:0.8, 7:0.34}
 
def function(dic):
   if not dic:
      return dic
   else:
      dic[100] = 0.1
      return dic
 
function(dic)
print(dic[100])
Reply
#3
Oh, ok, thank you so much
Reply
#4
Also note, as your function is altering the dictionary in place there is no need for the return statements.

You could just write this:

dic = {3:0.2, 4:0.5, 5:0.1, 6:0.8, 7:0.34}
  
def function(dic):
   if dic:
      dic[100] = 0.1
  
function(dic)
print(dic[100])
Reply
#5
Oh, I see,
but I'm actually working on another much more complicated code in which I need to call this function in main, maybe it's safer to do the return...
Anyway, thank you !
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  mutable argument in function definition akbarza 1 424 Dec-15-2023, 02:00 PM
Last Post: deanhystad
  calling external function with arguments Wimpy_Wellington 7 1,344 Jul-05-2023, 06:33 PM
Last Post: deanhystad
  Calling a function (which accesses a library) from another file mouse9095 4 767 Jun-07-2023, 08:55 PM
Last Post: deanhystad
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 1,026 Dec-25-2022, 03:00 PM
Last Post: askfriends
  passing dictionary to the function mark588 2 932 Dec-19-2022, 07:28 PM
Last Post: deanhystad
  i want to use type= as a function/method keyword argument Skaperen 9 1,775 Nov-06-2022, 04:28 AM
Last Post: Skaperen
  Error: _vhstack_dispatcher() takes 1 positional argument but 9 were given alexfrol86 3 5,717 May-09-2022, 12:49 PM
Last Post: deanhystad
Sad Iterate randint() multiple times when calling a function Jake123 2 1,979 Feb-15-2022, 10:56 PM
Last Post: deanhystad
  Calling a class from a function jc4d 5 1,755 Dec-17-2021, 09:04 PM
Last Post: ndc85430
  Regex - Pass Flags as a function argument? muzikman 6 3,484 Sep-06-2021, 03:43 PM
Last Post: muzikman

Forum Jump:

User Panel Messages

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