Kaartik Nayak
Kaartik Nayak

Reputation: 246

How to add values to a dictionary where it's key already exists

I have a for loop which gives me multiple lists containing 2 values. This is the for loop, the data is taken from an excel file. Problem arise when I want to add a value to a dictionary where it's key already exists

obj={}
test = ['Tonya Baker - Designer - noemail', ' Jess Huang - Designer - noemail', ' Denise Leung - Bizdev - noemail', ' Kristin Saulsbury - Bizdev - noemail', ' Molly Murphy - Bizdev - noemail', ' Angela Wood - Support - noemail', ' Heather Bond - Support - noemail', ' Natalie Ruiz - Support - noemail', ' Ivan Torres - Software - noemail', ' Max Gordon - Software - noemail', ' Jon Skulski - Software - noemail', ' Peggy Lin - Engineering - noemail', ' Andy Mai - Engineering - noemail', ' Alex Battaglino - Engineering - noemail', ' Evan Marks - Other - noemail', ' August Flanagan - Other - noemail', ' Helen Chi - Other - noemail', ' Ian Pearce - Other - noemail', ' Lisa Lamb - Marketing - noemail', ' Kate Levy - Marketing - noemail', ' Queen Tuba - Marketing - noemail', ' Crystal Baik - Marketing - noemail', ' Melissa Grant - Director - noemail', ' Scott Halcomb - Director - noemail', ' 
Victoria McCulloh - Manager - noemail', ' Shawn Warehouse - Manager - noemail', ' Shawn Norwood - Manager - noemail', ' Alex Bronson - Manager - noemail', ' Noah Solnick - Manager - noemail', ' Natalie Gordon - Csuite - noemail']
for i in test:
    arr = i.lstrip(' ').rstrip(' ').split(' - ')
    if arr[2] == 'noemail':
        arr = arr[:2]
    print(arr)  
# gives multiple arrays [['Tonya Baker', 'Designer'], ['Jess Huang', 'Designer']]
    obj[arr[1]] = arr[0]
print(obj)  # {'Designer': ['Jess Huang']}

But instead i want to it to be like :

{'Designer': ['Tonya Baker' ,'Jess Huang']}

Upvotes: 1

Views: 109

Answers (5)

Nothing special
Nothing special

Reputation: 415

Try this:

obj = {}
test = ['Tonya Baker - Designer - noemail', ' Jess Huang - Designer - noemail', ' Denise Leung - Bizdev - noemail', ' Kristin Saulsbury - Bizdev - noemail', ' Molly Murphy - Bizdev - noemail', ' Angela Wood - Support - noemail', ' Heather Bond - Support - noemail', ' Natalie Ruiz - Support - noemail', ' Ivan Torres - Software - noemail', ' Max Gordon - Software - noemail', ' Jon Skulski - Software - noemail', ' Peggy Lin - Engineering - noemail', ' Andy Mai - Engineering - noemail', ' Alex Battaglino - Engineering - noemail', ' Evan Marks - Other - noemail', ' August Flanagan - Other - noemail', ' Helen Chi - Other - noemail', ' Ian Pearce - Other - noemail', ' Lisa Lamb - Marketing - noemail', ' Kate Levy - Marketing - noemail', ' Queen Tuba - Marketing - noemail', ' Crystal Baik - Marketing - noemail', ' Melissa Grant - Director - noemail', ' Scott Halcomb - Director - noemail',
       'Victoria McCulloh - Manager - noemail', ' Shawn Warehouse - Manager - noemail', ' Shawn Norwood - Manager - noemail', ' Alex Bronson - Manager - noemail', ' Noah Solnick - Manager - noemail', ' Natalie Gordon - Csuite - noemail']
for i in test:
    arr = i.lstrip(' ').rstrip(' ').split(' - ')
    if arr[2] == 'noemail':
        arr = arr[:2]
    print(arr)
    if arr[1] not in obj:
        obj[arr[1]] = []
    obj[arr[1]] += [arr[0]]
print(obj)  

Output:

['Tonya Baker', 'Designer']
['Jess Huang', 'Designer']
['Denise Leung', 'Bizdev']
['Kristin Saulsbury', 'Bizdev']
['Molly Murphy', 'Bizdev']
['Angela Wood', 'Support']
['Heather Bond', 'Support']
['Natalie Ruiz', 'Support']
['Ivan Torres', 'Software']
['Max Gordon', 'Software']
['Jon Skulski', 'Software']
['Peggy Lin', 'Engineering']
['Andy Mai', 'Engineering']
['Alex Battaglino', 'Engineering']
['Evan Marks', 'Other']
['August Flanagan', 'Other']
['Helen Chi', 'Other']
['Ian Pearce', 'Other']
['Lisa Lamb', 'Marketing']
['Kate Levy', 'Marketing']
['Queen Tuba', 'Marketing']
['Crystal Baik', 'Marketing']
['Melissa Grant', 'Director']
['Scott Halcomb', 'Director']
['Victoria McCulloh', 'Manager']
['Shawn Warehouse', 'Manager']
['Shawn Norwood', 'Manager']
['Alex Bronson', 'Manager']
['Noah Solnick', 'Manager']
['Natalie Gordon', 'Csuite']
{'Designer': ['Tonya Baker', 'Jess Huang'], 'Bizdev': ['Denise Leung', 'Kristin Saulsbury', 'Molly Murphy'], 'Support': ['Angela Wood', 'Heather Bond', 'Natalie Ruiz'], 'Software': ['Ivan Torres', 'Max Gordon', 'Jon Skulski'], 'Engineering': ['Peggy Lin', 'Andy Mai', 'Alex Battaglino'], 'Other': ['Evan Marks', 'August Flanagan', 'Helen Chi', 'Ian Pearce'], 'Marketing': ['Lisa Lamb', 'Kate Levy', 'Queen Tuba', 'Crystal Baik'], 'Director': ['Melissa Grant', 'Scott Halcomb'], 'Manager': ['Victoria McCulloh', 'Shawn Warehouse', 'Shawn Norwood', 'Alex Bronson', 'Noah Solnick'], 'Csuite': ['Natalie Gordon']}

I saw the test value was added a while ago so I edited my answer.

Upvotes: 0

Kaartik Nayak
Kaartik Nayak

Reputation: 246

The answer :

from collections import defaultdict
d = defaultdict(list)
a = 'Tonya Baker - Designer - noemail; Jess Huang - Designer - noemail; Denise Leung - Bizdev - noemail; Kristin Saulsbury - Bizdev - noemail; Molly Murphy - Bizdev - noemail; Angela Wood - Support - noemail; Heather Bond - Support - noemail; Natalie Ruiz - Support - noemail; Ivan Torres - Software - noemail; Max Gordon - Software - noemail; Jon Skulski - Software - noemail; Peggy Lin - Engineering - noemail; Andy Mai - Engineering - noemail; Alex Battaglino - Engineering - noemail; Evan Marks - Other - noemail; August Flanagan - Other - noemail; Helen Chi - Other - noemail; Ian Pearce - Other - noemail; Lisa Lamb - Marketing - noemail; Kate Levy - Marketing - noemail; Queen Tuba - Marketing - noemail; Crystal Baik - Marketing - noemail; Melissa Grant - Director - noemail; Scott Halcomb - Director - noemail; Victoria McCulloh - Manager - noemail; Shawn Warehouse - Manager - noemail; Shawn Norwood - Manager - noemail; Alex Bronson - Manager - noemail; Noah Solnick - Manager - noemail; Natalie Gordon - Csuite - noemail'
test = a.lstrip(' ').rstrip(' ').split(';')
for s in test:
    name, role, *_ = s.split(' - ')
    d[role.strip()].append(name.strip())
da = dict(d)
print(da) # {'Designer': ['Tonya Baker', 'Jess Huang'], 'Bizdev': ['Denise Leung', 'Kristin Saulsbury', 'Molly Murphy'], 'Support': ['Angela Wood', 'Heather Bond', 'Natalie Ruiz'], 'Software': ['Ivan Torres', 'Max Gordon', 'Jon Skulski'], 'Engineering': ['Peggy Lin', 'Andy Mai', 'Alex Battaglino'], 'Other': ['Evan Marks', 'August Flanagan', 'Helen Chi', 'Ian Pearce'], 'Marketing': ['Lisa Lamb', 'Kate Levy', 'Queen Tuba', 'Crystal Baik'], 'Director': ['Melissa Grant', 'Scott Halcomb'], 'Manager': ['Victoria McCulloh', 'Shawn Warehouse', 'Shawn Norwood', 'Alex Bronson', 'Noah Solnick'], 'Csuite': ['Natalie Gordon']}

Upvotes: 0

deadshot
deadshot

Reputation: 9061

You can use itertools.groupby with dictionary comprehension

from itertools import groupby

data = sorted(test, key=lambda x: x.split('-')[1])
res = {k.strip(): [x.split('-')[0].strip() for x in g] for k, g in groupby(data, key=lambda x: x.split('-')[1])}
print(res)

Output:

{'Bizdev': ['Denise Leung', 'Kristin Saulsbury', 'Molly Murphy'],
 'Csuite': ['Natalie Gordon'],
 'Designer': ['Tonya Baker', 'Jess Huang'],
 'Director': ['Melissa Grant', 'Scott Halcomb'],
 'Engineering': ['Peggy Lin', 'Andy Mai', 'Alex Battaglino'],
 'Manager': ['Victoria McCulloh',
             'Shawn Warehouse',
             'Shawn Norwood',
             'Alex Bronson',
             'Noah Solnick'],
 'Marketing': ['Lisa Lamb', 'Kate Levy', 'Queen Tuba', 'Crystal Baik'],
 'Other': ['Evan Marks', 'August Flanagan', 'Helen Chi', 'Ian Pearce'],
 'Software': ['Ivan Torres', 'Max Gordon', 'Jon Skulski'],
 'Support': ['Angela Wood', 'Heather Bond', 'Natalie Ruiz']}

Upvotes: 0

imxitiz
imxitiz

Reputation: 3987

You can try this :

obj={}
test = ['Tonya Baker - Designer - noemail', ' Jess Huang - Designer - noemail', ' Denise Leung - Bizdev - noemail', ' Kristin Saulsbury - Bizdev - noemail', ' Molly Murphy - Bizdev - noemail', ' Angela Wood - Support - noemail', ' Heather Bond - Support - noemail', ' Natalie Ruiz - Support - noemail', ' Ivan Torres - Software - noemail', ' Max Gordon - Software - noemail', ' Jon Skulski - Software - noemail', ' Peggy Lin - Engineering - noemail', ' Andy Mai - Engineering - noemail', ' Alex Battaglino - Engineering - noemail', ' Evan Marks - Other - noemail', ' August Flanagan - Other - noemail', ' Helen Chi - Other - noemail', ' Ian Pearce - Other - noemail', ' Lisa Lamb - Marketing - noemail', ' Kate Levy - Marketing - noemail', ' Queen Tuba - Marketing - noemail', ' Crystal Baik - Marketing - noemail', ' Melissa Grant - Director - noemail', ' Scott Halcomb - Director - noemail', 'Victoria McCulloh - Manager - noemail', ' Shawn Warehouse - Manager - noemail', ' Shawn Norwood - Manager - noemail', ' Alex Bronson - Manager - noemail', ' Noah Solnick - Manager - noemail', ' Natalie Gordon - Csuite - noemail']

for i in test:
    arr = i.lstrip(' ').rstrip(' ').split(' - ')
    if arr[2] == 'noemail':
        arr = arr[:2]
    if arr[1] not in obj:
        obj[arr[1]] = []
    obj[arr[1]].append(arr[0])
print(obj)
"""
{'Designer': ['Tonya Baker', 'Jess Huang'],
 'Bizdev': ['Denise Leung', 'Kristin Saulsbury', 'Molly Murphy'],
 'Support': ['Angela Wood', 'Heather Bond', 'Natalie Ruiz'],
 'Software': ['Ivan Torres', 'Max Gordon', 'Jon Skulski'],
 'Engineering': ['Peggy Lin', 'Andy Mai', 'Alex Battaglino'],
 'Other': ['Evan Marks', 'August Flanagan', 'Helen Chi', 'Ian Pearce'],  
 'Marketing': ['Lisa Lamb', 'Kate Levy', 'Queen Tuba', 'Crystal Baik'],
 'Director': ['Melissa Grant', 'Scott Halcomb'],
 'Manager': ['Victoria McCulloh', 'Shawn Warehouse', 'Shawn Norwood', 'Alex Bronson', 'Noah Solnick'],
 'Csuite': ['Natalie Gordon']}
"""

We are just checking every time if there is already that key we are getting now or not in obj(dict) and if not we are creating it, after that we are appending that list.

Upvotes: 1

timgeb
timgeb

Reputation: 78690

Use a defaultdict.

>>> from collections import defaultdict
>>> test = ['Tonya Baker - Designer - noemail', ' Jess Huang - Designer - noemail', ' Denise Leung - Bizdev - noemail']    
>>> d = defaultdict(list)
>>> for s in test:
...    name, role, *_ = s.split(' - ')
...    d[role.strip()].append(name.strip())    
...
>>> d 
defaultdict(list,
            {'Designer': ['Tonya Baker', 'Jess Huang'],
             'Bizdev': ['Denise Leung']})

Upvotes: 2

Related Questions