Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/Stabilizing the error with the best answers: typeerror: cannot unpack non-iterable nonetype object
Next
Answered
Lyric Wang
  • 9
Lyric Wang
Asked: May 18, 20222022-05-18T20:20:39+00:00 2022-05-18T20:20:39+00:00In: python

Stabilizing the error with the best answers: typeerror: cannot unpack non-iterable nonetype object

  • 9

. Advertisement .

..3..

. Advertisement .

..4..

I’m building a new program, but when I run it, an error pops up. The error displayed is as follows:

Traceback (most recent call last):
  File "C:\Users\nehad\Desktop\Neha\Non-School\Python\Handwritten Digits 
 Recognition.py", line 38, in <module>
  X_train, y_train, X_test, y_test = load_dataset()
 TypeError: cannot unpack non-iterable NoneType object

I have tried several workarounds, but they still do not get the desired results. If you have come across this situation and have a solution for the “typeerror: cannot unpack non-iterable nonetype object” problem, pls let me know. Here is what I do:

import numpy as np
 
 
 def load_dataset():
  def download(filename, source="http://yaan.lecun.com/exdb/mnist/"):
  print ("Downloading ",filename)
  import urllib
  urllib.urlretrieve(source+filename,filename)
 
  import gzip
  
  def load_mnist_images(filename):
  if not os.path.exists(filename):
  download(filename)
  with gzip.open(filename,"rb") as f:
  data=np.frombuffer(f.read(), np.uint8, offset=16)
  
  data = data.reshape(-1,1,28,28)
  
  return data/np.float32(256)
 
  def load_mnist_labels(filename):
  if not os.path.exists(filename):
  download(filename)
  with gzip.open(filename,"rb") as f:
  data = np.frombuffer(f.read(), np.uint8, offset=8)
  return data
 
  X_train = load_mnist_images("train-images-idx3-ubyte.gz")
  y_train = load_mnist_labels("train-labels-idx1-ubyte.gz")
  X_test = load_mnist_images("t10k-images-idx3-ubyte.gz")
  y_test = load_mnist_labels("t10k-labels-idx1-ubyte.gz")
 
  return X_train, y_train, X_test, y_test
 
 
 X_train, y_train, X_test, y_test = load_dataset()
 
 
 import matplotlib
 matplotlib.use("TkAgg")
 
 import matplotlib.pyplot as plt
 plt.show(plt.imshow(X_train[3][0]))

Thanks!

cannot unpack non-iterable nonetype object
  • 2 2 Answers
  • 61 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    hdtutoria Expert
    2022-06-20T03:47:59+00:00Added an answer on June 20, 2022 at 3:47 am

    The cause: You get this error because X_train, y_train, X_test, y_test are defined inside in your load_mnist_images function and hence are not declared in your load_dataset function.

    Solution: De-indent your 5 lines from  X_train = ... to return X_train, ...  to fix this error. 

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Léa Menard
    2022-05-25T21:04:02+00:00Added an answer on May 25, 2022 at 9:04 pm

    This error occurs when you attempt to perform multiple assignments to None (which, by the way, is NoneType). Take this example:

    X_train, y_train, X_test, y_test = None

    TypeError: cannot unpack non-iterable NoneType object

    If you receive this, it is likely that you are wrong about the right-hand portion of the assignment. It is nothing.

    • 16
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.