user4480584
user4480584

Reputation:

Python: Unable to add a new key:value to a dictionary

I am making a dictionary for storing tests and their grades for different students.

def tests():
    test1 = {'craig':88, 'jeanie':100}
    test2 = {'craig':85, 'jeanie':95}
    test3 = {'craig':80, 'jeanie':98}
    return test1,test2,test3
def actions(test1,test2,test3):
    test1.update({'john':95})
    test1.update({'chris':92})
    test1.update({'charles',100})
    test2.update({'john':100})
    test2.update({'chris':96})
    test2.update({'charles',98})
    test3.update({'john':97})
    test3.update({'chris':100})
    test3.update({'charles',94})
    return test1,test2,test3
def main():
    one,two,three = tests()
    one,two,three = actions(one,two,three)
    print (test1,test2,test3)
main()

However, when I try to append a new key:value to my dicts two errors come up:

First:

Traceback (most recent call last):
  File "C:\file.py", line 26, in <module>
    main()
  File "C:\file.py", line 24, in main
    one,two,three = actions(one,two,three)
  File "C:\file.py", line 14, in actions
    test1.update({'charles',100})
TypeError: cannot convert dictionary update sequence element #0 to a sequence

Second:

Traceback (most recent call last):
  File "C:\file.py", line 26, in <module>
    main()
  File "C:\file.py", line 24, in main
    one,two,three = actions(one,two,three)
  File "C:\file.py", line 14, in actions
    test1.update({'charles',100})
ValueError: dictionary update sequence element #0 has length 7; 2 is required

If I run it over and over again, sometimes the first error comes up, sometimes the other.

I do not want any imports such as collections.

Upvotes: 0

Views: 4665

Answers (3)

user4516987
user4516987

Reputation:

If I understand your need you need add new values not update and for that operation you need change update for setdefault method. I have tested on Aptana Studio that code:

def tests():
    test1 = {'craig':88, 'jeanie':100}
    test2 = {'craig':85, 'jeanie':95}
    test3 = {'craig':80, 'jeanie':98}
    return test1,test2,test3
def actions(test1,test2,test3):
    test1.setdefault('john',95)
    test1.setdefault('chris',92)
    test1.setdefault('charles',100)
    test2.setdefault('john',100)
    test2.setdefault('chris',96)
    test2.setdefault('charles',98)
    test3.setdefault('john',97)
    test3.setdefault('chris',100)
    test3.setdefault('charles',94)
    return test1,test2,test3
def main():
    one,two,three = tests()
    one,two,three = actions(one,two,three)
    print(one,two,three)
main()

and get response:

one - {'john': 95, 'charles': 100, 'jeanie': 100, 'chris': 92, 'craig': 88} 
two - {'john': 100, 'charles': 98, 'jeanie': 95, 'chris': 96, 'craig': 85} 
three - {'john': 97, 'charles': 94, 'jeanie': 98, 'chris': 100, 'craig': 80}

Your problem is that update search one dictionary with key for update your value and not insert but setdefault insert new pair key:value with that syntax case not exists and return the value for one key case her exists.

Good work for you,

Upvotes: 1

datasci
datasci

Reputation: 1029

See answer here:

Add new keys to a dictionary?

To update:

#### Inserting/Updating value ####
data['a']=1  # updates if 'a' exists, else adds 'a'
# OR #
data.update({'a':1})
# OR #
data.update(dict(a=1))
# OR #
data.update(a=1)
# OR #
data.update([(a,1)])

Instead of this:

test3.update({'charles',94})

Upvotes: 0

Joran Beasley
Joran Beasley

Reputation: 113988

test1.update({'charles',100})

is updating the dict with a set not a dict, which it clearly cannot use to update ... instead of sets pass it dicts

test1.update({'charles':100})

just to demonstrate

{1,2,3,4,4,5,6}   # a set that will contain 1,2,3,4,5,6
{1:2,3:4,4:5}   # a dict with 3 elements dict(1=2,3=4,4=5)

Upvotes: 4

Related Questions