Python Dictionary Error Too Many Values To Unpack
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this valueerror: too many values to unpack list site About Us Learn more about Stack Overflow the company Business Learn more
Too Many Values To Unpack Dictionary
about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x
Too Many Values To Unpack Python 3
Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up 'too many
Python Too Many Values To Unpack (expected 2)
values to unpack', iterating over a dict. key=>string, value=>list up vote 112 down vote favorite 19 I am getting the 'too many values to unpack' error. Any idea how I can fix this? first_names = ['foo', 'bar'] last_names = ['gravy', 'snowman'] fields = { 'first_names': first_names, 'last_name': last_names, } for field, possible_values in fields: # error happens on this line python share|improve this question python split too many values to unpack edited Mar 29 '13 at 7:53 jamylak 56.7k11129157 asked Mar 29 '11 at 0:32 tipu 2,61973882 add a comment| 3 Answers 3 active oldest votes up vote 169 down vote accepted You need to use something like iteritems. for field, possible_values in fields.iteritems(): print field, possible_values See this answer for more information on iterating through dictionaries, such as using items(), across python versions. share|improve this answer edited Feb 28 '13 at 10:45 Cyrille 1,023813 answered Mar 29 '11 at 0:34 Philip Southam 7,30752019 29 As for why you need to do this, iterating over a dictionary iterates through the keys, not the key/value pairs. –Jeff Mercado Mar 29 '11 at 0:35 1 @jeffm that's what confused me. i saw this as a PHP foreach. –tipu Mar 29 '11 at 16:23 add a comment| up vote 26 down vote For Python 3.x iteritems has been removed. Use items instead. for field, possible_values in fields.items(): print(field, possible_values) share|improve this answer edited Jun 24 at 17:31 arielf 2,95111335 answered Mar 3 '15 at 21:48 Meistro 1,0471018 add a comment| up vote 25 down vote You want to use iteritems. This
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and python requests too many values to unpack policies of this site About Us Learn more about Stack Overflow the python too many values to unpack function company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users cv2 findcontours too many values to unpack Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes http://stackoverflow.com/questions/5466618/too-many-values-to-unpack-iterating-over-a-dict-key-string-value-list a minute: Sign up Too many values to unpack in Python dictionary value split()? up vote 0 down vote favorite I have a dictionary called speech_dict where key is a file's name and value is a speech (usually about 10,000 words). I'm trying to get the stem of each word; this is accomplished through some functions in the nltk module. Here's the http://stackoverflow.com/questions/33791253/too-many-values-to-unpack-in-python-dictionary-value-split code I have: from __future__ import print_function from nltk.stem import * from nltk.stem.snowball import SnowballStemmer stemmer = SnowballStemmer("english") spee_dict = speech_dict for key,value in spee_dict: for i in value.split(): i = stemmer.stem(i) i = i.decode('utf-8') spee_dict[key].add(i) I get the following error: ValueError: too many values to unpack There are about 900 keys stored in speech_dict, and about 3.5 million words total to go through. How can I make this work? Edit: Alright, Delgan helped me add the stemmed word. How can I remove the original word, now? I have this: for key,value in spee_dict.items(): for i in value.split(): i_stemmed = stemmer.stem(i) i_stemmed = i_stemmed.decode('utf-8') spee_dict[key] += i_stemmed spee_dict[key] - I But the - operand isn't supported by Unicode. python python-2.7 for-loop text nltk share|improve this question edited Nov 18 '15 at 22:15 asked Nov 18 '15 at 22:00 not_a_robot 896315 are you sure speech_dict is a dictionary, show how you defined it –Assem Chelli Nov 18 '15 at 22:05 Note that including the line-number the error corresponds to is very helpful. (Even though it's fairly obvious here). –DilithiumMatr