2

I want to create a new dictionary (new_dict), from the old dictionary(my_dict), in new_dict values of the my_dict will be keys and keys will be values.

Expected output:

{"name" : [1, 3, 5], "last_name": [2, 4, 6]} 

But, I am getting:

{"name" : [1, 2, 3, 4, 5, 6], "last_name": [1, 2, 3, 4, 5, 6]} 

This is my code:

def my_function(my_dict): # Creating new dictionary new_dict = dict.fromkeys(set(my_dict.values()), []) for key, value in my_dict.items(): new_dict[value].append(key) return new_dict # Existing dictionary my_dict = { 1: "name", 2: "last_name", 3: "name", 4: "last_name", 5: "name", 6: "last_name"} res = my_function(my_dict) print(res) 

6 Answers 6

6

This line:

new_dict.fromkeys(set(my_dict.values()), []) 

creates a dictionary with the very same list as a value for each key, so later you append to the same list, too.

You can use dict comprehension in this case:

new_dict = { k: [] for k in my_dict.values() } 
Sign up to request clarification or add additional context in comments.

Comments

3

You can use collections.defaultdict(list) to create a dictionary of list values:

import collections my_dict = { 1: "name", 2: "last_name", 3: "name", 4: "last_name", 5: "name", 6: "last_name", } d = collections.defaultdict(list) for num, text in my_dict.items(): d[text].append(num) 

You get what you want.

See the documentation here: https://docs.python.org/3/library/collections.html#collections.defaultdict

Quoting an example:

>>> s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)] >>> d = defaultdict(list) >>> for k, v in s: ... d[k].append(v) ... >>> sorted(d.items()) [('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])] 

1 Comment

The OP wanted a function.
2

Here is what you can do:

my_dict = {1: "name", 2: "last_name", 3: "name", 4: "last_name", 5: "name", 6: "last_name"} def my_function(my_dict): new_dict = {v:[k for k in my_dict.keys() if my_dict[k] == v] for v in my_dict.values()} return new_dict print(my_function(my_dict)) 

Output:

{'name': [1, 3, 5], 'last_name': [2, 4, 6]} 

Comments

1

If the whole data is in this format, Your can use the odd/even property of the keys:

def func(my_dict): new_dict = {} new_dict['name'] = {} new_dict['last_name'] = {} for i in my_dict.keys(): if i%2 == 1 : new_dict['name'].append(i) else : new_dict['last_name'].append(i) return new_dict 

1 Comment

Thanks for the answer, but, there is no such constraint that dictionary values will be in an odd/ even manner.
1

You can either use collections.defaultdict, or use dict.setdefault. There is already an answer using defaultdict, so I show the second option:

new_dict = {} for k, v in my_dict.items(): vv = new_dict.setdefault(v, []) vv.append(k) 

2 Comments

The OP wanted a function.
I hope they are able to write a function based on this code... Oh I see, you are just trolling every answer.
-1

Apologize for my previous ans. Here is the updated one. It can achieve the result though it needs improvement. Share your thoughts for optimization, please :)

new_dict = {v: [x for x, y in my_dict.items() if y == v] for k, v in my_dict.items() } 

{'name': [1, 3, 5], 'last_name': [2, 4, 6]}

2 Comments

This does not work because it is necessary to match multiple k values to the same v key, and accumulate them in a list.
@KarlKnechtel please check now

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.