473,473 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

ValueError: too many values to unpack

2 New Member
Hello all!
I think there are some people here to help me
Of course I did some search, similar topic (that I walked through) did not discuss for loops
My problem is this error: ValueError: too many values to unpack (in line 4)

I have a function for getting the average of a dictionary!
(sorry! new to python)
I have summarized the code and printed the variable sumPerYear so you can have a quick look:
Expand|Select|Wrap|Line Numbers
  1. def average(array):
  2.     total=0.0
  3.     size=len(array)
  4.     for (name,item) in array:
  5.         print item
  6.         total=total+float(item)
  7.  
  8.     avg=total/size
  9.     return avg
  10.  
  11.  
  12. sumPerYear={'1999': 3723.18, '1995': 3999.05, '1997': 4225, '2005': 3946, '1991': 4191, '2003': 3862, '1993': 4377.27, '2001': 3509.27, '1998': 3738.64, '1994': 3952.5, '2004': 3905., '1996': 3859.59, '2002': 3865.77, '1990': 4172.64, '2000': 3853.36, '1992': 4708.95}
  13. print(average(sumPerYear))
Many thanks for you help
Sep 30 '09 #1
1 4949
JDCyrus
3 New Member
If you iterate over a dictionary, it will iterate over just the keys, not key-value tuples. You want to say "for name in array:" and then "print array[name]" and so forth. Alternatively, you could say "for (name, item) in array.items():" and then the rest of your code would work the way you want it to, since the dict.items method does return key-value tuples. The first way is more efficient, though.

EDIT: You can actually accomplish all of this much more efficiently and with a lot less code. Try this:
Expand|Select|Wrap|Line Numbers
  1. def average(array):
  2.     return float(sum(array.values())) / len(array)
Sep 30 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Raoul | last post by:
I wrote a COM server object in C++ a few months ago. I can use it from Visual Basic, Visual C++, S-Plus and a number of other scripting environments. What I can't do is use it with my FAVOURITE...
8
by: Paul McGuire | last post by:
I'm trying to manage some configuration data in a list of tuples, and I unpack the values with something like this: configList = for data in configList: name,a,b,c = data ... do something...
5
by: Geoffrey | last post by:
Hope someone can help. I am trying to read data from a file binary file and then unpack the data into python variables. Some of the data is store like this; xbuffer:...
3
by: Elezar Simeon Papo | last post by:
Hello All, I have a tab separated input file (data.txt) in text format - the file looks like this SCHOOL DEPART1 DEPART2 DEPART3 Harvard Economics Mathematics Physics...
2
by: k.retheesh | last post by:
Hi, I am very new to pyton, during my adventures journey I got the following error message which am not able to solve, can somebody help me. I was trying to format my output in a readable way, ...
0
by: laxmikiran.bachu | last post by:
ValueError: need more than 1 value to unpack . This is the error I get when I am using pyRXPU in the one of the scripts to get the strings from an application. Can anybody throw some light on...
2
by: Shawn Minisall | last post by:
I'm trying to unpack a list of 5 floats from a list read from a file and python is telling me 5 variables are too many for the string.split statement. Anyone have any other idea's? NOTE: the only...
8
by: Shawn Minisall | last post by:
I am trying to read a few lines of a file with multiple values, the rest are single and are reading in fine. With the multiple value lines, python says this "ValueError: too many values to...
2
by: brnstrmrs | last post by:
If I run: testValue = '\x02\x00' junk = struct.unpack('h', testValue) Everything works but If I run testValue = raw_input("Enter Binary Code..:") inputting at the console '\x02\x00' junk...
0
by: Ping Zhao | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I am writing a small program to decode MS bitmap image. When I use statements as follow, it works fine: header = str(struct.unpack('2s',...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.