Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

iteration over non-sequence error in dictionary.keys()

Status
Not open for further replies.

ruuram

Programmer
Sep 15, 2005
1
US

I'm new to Python. When I try to run the following program with
a3=DictAdder()
a3.add({1:1},{2:2}}
I get the "iteration over non-sequence" error.

class DictAdder:
def add(self, x, y):
dict={}
for k in x.keys(): dict[k]=x[k]
for k in y.keys(): dict[k]=y[k]
return dict

does anyone know what's wrong with the code? or I need to import some libraries? Thanks!
 
I don't see the problem.

When I run this:
Code:
class DictAdder:
    def add(self, x, y):
        dict={}
        for k in x.keys(): dict[k]=x[k]
        for k in y.keys(): dict[k]=y[k]
        return dict


a3=DictAdder()
print a3.add({1:1},{2:2})
I get this:
{1: 1, 2: 2}

I'm assuming the closing brace in your original post:
Code:
a3.add({1:1},{2:2}}
                  ^
is a typo.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top