Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
dictionary merge
#4
There is no need to create a merged dict, just to unpack it in function call. Isn't that what you actually want:
spam = {'a':1, 'b':2}
eggs = {'c':3, 'd':4}

def foo(*args, **kwargs):
    for key, value in kwargs.items():
        print(f'{key} -> {value}')

foo(**spam, **eggs)
Output:
a -> 1 b -> 2 c -> 3 d -> 4
or to extend it with positional arguments:

spam = {'a':1, 'b':2}
eggs = {'c':3, 'd':4}

def foo(*args, **kwargs):
    for arg in args:
        print(arg)
    for key, value in kwargs.items():
        print(f'{key} -> {value}')

foo('boo', 'woo', **spam, **eggs)
Output:
boo woo a -> 1 b -> 2 c -> 3 d -> 4
or
spam = {'a':1, 'b':2}
eggs = {'c':3, 'd':4}
bar = ['boo', 'woo']

def foo(*args, **kwargs):
    for arg in args:
        print(arg)
    for key, value in kwargs.items():
        print(f'{key} -> {value}')

foo(*bar, **spam, **eggs)
Output:
boo woo a -> 1 b -> 2 c -> 3 d -> 4
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
dictionary merge - by Skaperen - Jan-16-2020, 02:19 AM
RE: dictionary merge - by snippsat - Jan-16-2020, 04:04 AM
RE: dictionary merge - by Skaperen - Jan-16-2020, 07:11 AM
RE: dictionary merge - by buran - Jan-16-2020, 07:50 AM
RE: dictionary merge - by perfringo - Jan-16-2020, 08:05 AM
RE: dictionary merge - by Skaperen - Jan-17-2020, 01:28 AM

Forum Jump:

User Panel Messages

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