Python: Solving ValueError: Too many values to unpack
Skills:
Python for Data90%
Key Takeaways
This video tutorial by sentdex covers the ValueError: too many values to unpack in Python, caused by having more values to unpack than variables specified, and provides solutions by specifying a matching number of variables to be read into, using examples with lists and numpy arrays.
Full Transcript
Hello everybody. Welcome to another Python video tutorial. In this video, what we're going to be talking about is this value error. Too many values to unpack. What is it? Uh what do we do to get this and what do we do about it? So, uh the easiest thing to show you guys is just by example. So, literally, I'll just call it example. And the way that you're going to get this is when you try to unpack variables from like a li something like a list in python you also will get it uh whenever you're unpacking say a numpy array for example same same story here. So example as you can see this example is a you know it's comma delimited it's got you know if you said example and then brackets zero oops zero like this it would correspond to one one would be number three two would be number four and so on and so when you unpack you can unpack these values into so instead of saying like um let's say x equals example zero you can unpack packed and then like y right equals example one and so on. You can unpack the val uh the values straight into the variables really quickly by doing something like this where x y uh equals example. And now the problem with this though is we have four values here and we're only specifying two. So let's go ahead and specify even three for example. So what it's going to attempt to do is it's going to attempt to take all the values here and just like store them into these variables. But the problem is it has one too many. So what you'll get when you run this is the following right value error too many values to unpack. But if you set xyz uh a and then you run this you won't have any problems. It's fine. And then when you go later to you know access these variables like print x print y print z print a oops print a and you run that you know they're stored. And so the other time that you'll see it and what you wouldn't want to get confused by is for example lists within a list uh like this. So let's say x is 1 2 3 4 5 y is um three. We're going to run off the screen. So let me just give them like three variables in each list. Three, five, seven. And then z and a can just have one one value. Let's say um so you can still do this, right? So, so when I run this, um, there you go. You've got, you know, this new list, right? So, anyway, um, that's basically basically all it is to this whole value error. Too many values to unpack. Basically, you just tried to unpack uh from some sort of variable that had too many values uh corresponding to how many variables you were feeding it. So, that's pretty much um all you need to do is just add another variable, figure out what you're missing in the list. So anyways, uh hopefully that was useful for some of you guys. As always, thanks for watching and thanks for all the support and subscriptions.
Original Description
This Python tutorial video covers the "ValueError: too many values to unpack."
This error is caused when there are more values in whatever is being unpacked than the amount of variables you have specified to be read into. The solution is to specify a matching number of variables to be read into as are being unpacked.
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from sentdex · sentdex · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
Python Encryption Tutorial with PyCrypto
sentdex
Python's Logging Function
sentdex
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
wxPython Programming Tutorial 4: Panels
sentdex
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
wxPython Programming Tutorial 8: Custom Button Images
sentdex
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
Basic PHP Tutorial 12: Associative Array
sentdex
Basic PHP Tutorial 14: Foreach loop
sentdex
Basic PHP Tutorial 16: Include and Require
sentdex
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
Basic PHP Tutorial 4: Variables and Comments
sentdex
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
Basic PHP Tutorial 1: Intro to PHP
sentdex
Basic PHP Tutorial 3: HTML with PHP
sentdex
Basic PHP Tutorial 9: While Loop
sentdex
Basic PHP Tutorial 10: Switch Statement
sentdex
Basic PHP Tutorial 2: Print and Echo
sentdex
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
Basic PHP Tutorial 19: Finding words in strings
sentdex
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
Basic PHP Programming Tutorial 24: String similarity
sentdex
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
Sikuli Tutorial 1: Visually programming in python!
sentdex
Sikuli Tutorial 2: Program visually in python!
sentdex
Sikuli Tutorial 3: Program visually in python!
sentdex
3D Bar Charts in Python and Matplotlib
sentdex
3D Plane wire frame Graph Chart in Python
sentdex
Raspberry Pi Part 1 Introduction
sentdex
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
Raspberry Pi Part 11: Remote Desktop
sentdex
Twitter Analysis: How to rank a user's influence
sentdex
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex
More on: Python for Data
View skill →
🎓
Tutor Explanation
DeepCamp AI