How to split a dictionary? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How to split a dictionary?

I want to split a dictionary: Dict1 = {1 : "dallas", 2 : "texas", 3 : "California" , 4 : "New Orleans"} How do I split it into a half so that the result is: Dict1 = {1: "dallas" , 2: "texas"} Dict2 = {3 : "California" , 4 : "New Orleans"}

24th Sep 2021, 12:02 PM
Richard
Richard - avatar
17 Answers
+ 6
well in short you can't, but to do this you'll have to use a list to separate dict which means that the splitting will be the order of the original dict, if you have like data = {2: "foo", 1: "bar"} the splitted dict will be in that order. So back to the splitting, dict have a method called "items()" which returns an iterator that contains (key, value) format on each elements so we want to take that in a list like items = list(Dict1.items()) # we use list() because items() returns an iterator instead of a list now we have the dict converted to a list and we can go ahead and split the list. items = list(Dict1.items()) center = len(items)//2 Dict1 = dict(items[:center]) Dict2 = dict(items[center:]) and now we have the dict splitted yay! also this code only works if the dict has even elements like 10 or 2 elements
24th Sep 2021, 12:24 PM
CutieRei
CutieRei - avatar
+ 4
also a suggestion: don't use PascalCase for normal variable instead use snake_case so instead of Dict1 you would use dict1. NEVER USE camelCase IN PYTHON
24th Sep 2021, 1:45 PM
CutieRei
CutieRei - avatar
+ 3
and extra information: __setitem__ doesn't call update and update doesn't call __setitem__
25th Sep 2021, 11:42 AM
CutieRei
CutieRei - avatar
+ 2
Rei, I tried your logic in a code and it works really well 👍 It also works for odd number of items, even without the check for odd/even number of items. When dictionary has odd number of items, the second split will get more items than the first. By incrementing <center> the second split will get more. Can't wait for the OP to try it out!
24th Sep 2021, 3:49 PM
Ipang
+ 2
Well provided that the length is divisible by 2, try this. def dict_split(dictionary): dict1 = dict() dict 2 = dict () for key, value in dictionary.items(): if len(dict1) < len(dictionary)/2: dict1.update({key: value}) else: dict2.update({key: value}) return dict 1, dict2 def main(): dictionary = {1:"dallas", 2:"texas", 3:"California, 4 : " Orleans"} dict1, dict2 = dict_split(dictionary) print (dict1) print (dict2) if __name__ == "__main__": main()
24th Sep 2021, 7:26 PM
🌼Faith🌼
🌼Faith🌼 - avatar
+ 1
example: if (len(items) % 2) != 0: center += 1
24th Sep 2021, 1:54 PM
CutieRei
CutieRei - avatar
+ 1
Rei why not camelCase?
24th Sep 2021, 3:34 PM
Oma Falk
Oma Falk - avatar
+ 1
Or something like this: d1 = {"e":0, "f":0} print(d1.items()[0:(len(d1)/2)]) print(d1.items()[(len(d1)/2)::]) if items %2!=0 parts of same size not possible, as far I get it. I tryed with 3 elements in dict, it split it in groups of 1 and 2 elements.
24th Sep 2021, 5:48 PM
Sven_m
Sven_m - avatar
+ 1
Nwachi Faith btw its kinda not useful using dict.update() at this case, since we're just setting a key might as well just dict1[key] = value
25th Sep 2021, 1:35 AM
CutieRei
CutieRei - avatar
+ 1
Nwachi Faith but update is supposed to be similar to list.extend, for adding a single element to a dict by using dict.update is overkill, you're better off with dict[key] = value, unless you already have a dict you want to use or multiple key to map
25th Sep 2021, 11:39 AM
CutieRei
CutieRei - avatar
0
What should it be like when the original dictionary contains odd number of items?
24th Sep 2021, 1:50 PM
Ipang
0
well it'll be the same except you swap the assignment, so Dict1 is Dict2 and Dict2 is Dict1
24th Sep 2021, 1:52 PM
CutieRei
CutieRei - avatar
0
oh wait nevermind that's bad
24th Sep 2021, 1:53 PM
CutieRei
CutieRei - avatar
0
okay so basically you can check if the length of the items is an odd number so then we can add 1 to the center variable
24th Sep 2021, 1:54 PM
CutieRei
CutieRei - avatar
0
oh my bad
24th Sep 2021, 1:59 PM
CutieRei
CutieRei - avatar
0
Oma Falk because python naming convention, for classes its mandatory to use PascalCase while normal variable or functions are snake_case camelCase are never used in python except for old modules, and using camelCase is generally discouraged in python
25th Sep 2021, 12:51 AM
CutieRei
CutieRei - avatar
0
Rei It does the same thing. update () method also adds key: value pairs to the dictionary as much dict [key] = value does. So win win.
25th Sep 2021, 8:15 AM
🌼Faith🌼
🌼Faith🌼 - avatar