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/Resolved the problem of the valueerror: max() arg is an empty sequence
Next
Answered
Lachlan Gagnon
  • 7
Lachlan Gagnon
Asked: May 18, 20222022-05-18T16:00:44+00:00 2022-05-18T16:00:44+00:00In: python

Resolved the problem of the valueerror: max() arg is an empty sequence

  • 7

. Advertisement .

..3..

. Advertisement .

..4..

I have the following python code, but I do not know how to find the correct result. Why has this problem occurred, and how can it be solved?
Here is the code that I am running:

import wx
 import myLoopGUI
 import commands
 
 class MyLoopFrame(myLoopGUI.MyFrame1):
  def __init__(self, parent):
  myLoopGUI.MyFrame1.__init__(self, parent)
 
  def clkAddData(self,parent):
  if len(self.txtAddData.Value) != 0:
  try:
  myname = str(self.txtAddData.Value)
  self.listMyData.Append(str(myname))
  except:
  wx.MessageBox("This has to be a name!") 
  else:
  wx.MessageBox("This can't be empty")
 
 
 
 
  def clkFindMost(self, parent):
  self.listMyData = []
  unique_names = set(self.listMyData)
  frequencies = {}
  for name in unique_names:
  if frequencies.get[name]:
  frequencies[name] += 1
  else:
  frequencies[name] = 0
 
  v = list(frequencies.values())
  k = list(frequencies.keys())
  self.txtResults.Value = k.index(max(v))
 
 
  def clkFindLeast(self, parent):
  unique_names = set(self.listMyData)
  frequencies = {}
  for name in unique_names:
  if frequencies.get(name):
  frequencies[name] += 1
  else:
  frequencies[name] = 0
 
  v = list(frequencies.values())
  k = list(frequencies.keys())
  self.txtResults.Value = k.index(min(v))
 
 myApp = wx.App(False)
 myFrame = MyLoopFrame(None)
 myFrame.Show()
 myApp.MainLoop()

And this is the error text I receive:

ValueError: max() arg is an empty sequence

valueerror
  • 2 2 Answers
  • 157 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    dttutoria Expert
    2022-06-14T08:26:36+00:00Added an answer on June 14, 2022 at 8:26 am

    The cause: Because in clkFindMost, you always set self.listMyData to an empty list. As both unique_names and frequencies are empty iterables after that, your code will always result in this error.
    In addition, you can’t use [] with dict.get because it’s a method, not a list/dictionary:

    Solution:

    To fix “valueerror: max() arg is an empty sequence”, you can follow my methods:

    Run the right code using () instead of []

    if frequencies.get(name):

    It returns an error, when an empty iterable is supplied to max() and min(). Before running max() on v, you can check its length.

    >>> lst = []
    >>> max(lst)
    
    Traceback (most recent call last):
    File "<pyshell#2>", line 1, in <module>
    max(lst)
    ValueError: max() arg is an empty sequence
    >>> if lst:
    mx = max(lst)
    else:
    #Handle this here

    If it is being used with an iterator, you’ll need to consume it first before executing max() on it, while the iterator’s boolean value is always True, so we can’t use if instantly. Do like the following:

    >>> it = iter([])
    >>> bool(it)
    True
    >>> lst = list(it)
    >>> if lst:
    mx = max(lst)
    else:
    #Handle this here

     

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Adam Hardy
    2022-05-25T20:27:48+00:00Added an answer on May 25, 2022 at 8:27 pm

    If the sequence is empty, pass a default value that can be returned to max by passing:

    max(v, default=0)
    • 24
    • 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.