Python Forum
What is the best way to add entries from 1 dictionary to another?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is the best way to add entries from 1 dictionary to another?
#5
Update is expecting a sequence, each element of which is a mapping (or a sequence of length 2). So you can't feed it a bare tuple, but you can feed it a list of tuples or a dict created from the tuple.

d = {}
item = ('a', 1)

d.update(item)   # won't work
d.update([item]) # okay
d.update({item}) # also okay
That suggests that since it has to take a sequence anyway, you could create one by slicing your original dict rather than using a for loop.

from itertools import islice

groupA = dict(islice(rev_sorted_dataDict.items(), 0, 38) # or....
groupB = {}
groupB.update(islice(rev_sorted_dataDict.items(), 38, 75)
Pedroski55 likes this post
Reply


Messages In This Thread
RE: What is the best way to add entries from 1 dictionary to another? - by bowlofred - Oct-21-2020, 12:39 AM

Forum Jump:

User Panel Messages

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