0

I am trying to merge three dictionaries together.

I am receiving an unsupported operand types error.

Here is my code:

def add_student():
    global Snumber
    global iCode
    global kCode
    Snumber = Student_number.get()
    Sname = Student_name.get()
    Ssurnname = Student_surname.get()
    Sdetail = Student_detail.get()
    i = Students(Snumber,Sname,Ssurnname,Sdetail)
    Sinfo[Snumber]=[Sname,Ssurnname,Sdetail]

   iName = Student_subject.get()
   iCode = Student_code.get()
   iMark1 = Student_Mark1.get()
   iMark2 = Student_Mark2.get()
   iMark3 = Student_Mark3.get()
   iProject = Student_project.get()
   j = Subjects(iName,iCode,iMark1,iMark2,iMark3,iProject)
   SSubject[iCode]=[iName,iMark1,iMark2,iMark3,iProject]

  kCourse = Degree_course.get()
  kCode = Degree_code.get()
  kYear = Degree_year.get()
  v = Degrees(kCourse,kCode,kYear)
  SDegree[kCode]=[kCourse,kYear]

  popup_add()
  student_list = (Sinfo.items() + SSubject.items() + SDegree.items())
  print(student_list)

I believe my problem is in:

  student_list = (Sinfo.items() + SSubject.items() + SDegree.items())
  print(student_list)
jwpfox
  • 5,124
  • 11
  • 45
  • 42
  • 2
    This example looks incomplete to me. What is e.g. `SInfo`? – languitar Nov 09 '16 at 14:35
  • In the question title you say merging dictionaries, in the first line of your question you say they are lists, then at the end you say they are dictionaries. You need to be a bit clearer. – DavidG Nov 09 '16 at 14:37
  • can you print type(Sinfo) (and other dicts) and len(Sinfo.items()) (and other items? ) – Illusionist Nov 09 '16 at 14:37
  • Possible duplicate of [merging "several" python dictionaries](http://stackoverflow.com/questions/9415785/merging-several-python-dictionaries) – John Smith Nov 12 '16 at 00:20

2 Answers2

1

you can use dict.update()

>>> a = {1:1,2:2,3:3}
>>> a
{1: 1, 2: 2, 3: 3}
>>> b = {4:4,5:5}
>>> c = {6:6,7:7}
>>> a.update(b)
>>> a.update(c)
>>> a
{1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7}

if you dont want to modify the original you can use the following to copy it into a new variable

>>> new_dict = dict(a)
  • That is correct, however in the case of the OP's problem, it appears that is the case. But yes, the keys do need to be unique, if they aren't then you'd need to write your own merging function to handle duplicates the way you want. – Max Chesterfield Nov 12 '16 at 14:29
0

To merge multiple dictionary, lets say we have dict Sinfo, SSubject and SDegree

student_list = dict(Sinfo.items() + SSubject.items() + SDegree.items())

code above will works with python 2 only. for python 3, need to add list to convert dict.items into list first as shown below

student_list = dict(list(Sinfo.items()) + list(SSubject.items()) + list(SDegree.items()))
Skycc
  • 3,496
  • 1
  • 12
  • 18
  • The unsupported operand type I get is: `TypeError: unsupported operand type(s) for +: 'dict_items' and 'dict_items'`. Have you ever tried your suggestion? Just curious, as this error seemingly does not involve the contents of the dicts, it simply complains that dict.items() cannot be added. – Bernd Wechner Aug 13 '22 at 05:22
  • i guess thats because you are using python3, python 3 dict items is no longer a list, so can't add directly, update the answer for python 3 – Skycc Aug 15 '22 at 01:02