Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"Unhashable type: list"
#1
Method_A expects parameters in the below standard. The final argument, arg3, accepts one or more values
Method_A(arg1 , arg2, arg3)

I have a list which contains the below data
my_list[0] = "hello"
my_list[1] = "world"
Method_A works fine when I pass arguments in the below fashion
Method_A(arg1, arg2, my_list[0], my_list[1])
But, when i pass the entire list to method_A,
Method_A("", "", my_list)
I observe "Unhashable type: list" as error.

Any suggestions?
Reply
#2
lists are mutable, thus not hashable. Make my_list tuple.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
i tried converting to tuple, does not work still

Just to explain further,

arg[0] = "10"
arg[1] = "20"
arg[2] = "30"

when i pass these values individually to my method, the function works fine
Ex : Method(arg[0], arg[1], arg[2])

But when is send the list as argument, i see "Unhashable type: List"
Ex: Method(arg)
Reply
#4
post your actual code. if you converted the list to tuple why do you still get unhashable type list error?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
it's difficult to tell from information provided, but you may need to unpack your list.
e.g. method(*arg)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
*******python_file1.py***********
in this python_file1, i am setting up data to invoke main method present in python_file2

......
node_id = get_node_id(node)

test_case[0] = "some_value0"
test_case[1] = "some_value1"
test_case[2] = "some_value2"

===> Invoking main method
python_file2.main(["h","--node-id="+ node_id, "run", test_case[0], test_case[1], test_case[2], "--force"])
THis call to main method works absolutely fine. But when i pass the complete list or convert it into a tuple and pass it to main, i see Unhashable error

python_file2.main(["h","--node-id="+ node_id, "run", test_case, "--force"] ===> error



**********python_file2.py************
def main(argv):
parser = argparse.ArgumentParser
parser.add_argument("-C", metavar="PATH")
parser.add_argument("--node-id", metavar="stb-tester-abcdef123456")
subcommands = parser.add_subparsers(
dest="command", title="COMMANDS", metavar="COMMAND",
description=dedent("""\
Note: Run "./stbt_rig.py COMMAND -h" to see the additional
parameters for each COMMAND."""))
run_parser = subcommands.add_parser("run", help="Run test-cases")
run_parser.add_argument("test_cases", nargs='+', metavar="TESTCASE")
run_parser.add_argument("--force", action="store_true")

args = parser.parse_args(argv[1:])
......
Reply
#7
node_id = get_node_id(node)
node_cmd = "--node-id={}".format(node_id)
test_case = ["some_value0", "some_value1", "some_value2"]
cmd = ["h", node_cmd, "run"]
cmd.extend(test_case)
cmd.append("--force")
print cmd
python_file2.main(cmd)
something like this. other options are also possible
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
Wow ! That worked !!! Thank you :)
Reply
#9
something I don't like, but would so work
python_file2.main(["h","--node-id="+ node_id, "run"] + test_case + ["--force"])
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unhashable error - Histogram code lsbpython 1 996 Aug-07-2022, 04:02 PM
Last Post: Yoriz
  search a list or tuple for a specific type ot class Skaperen 8 1,946 Jul-22-2022, 10:29 PM
Last Post: Skaperen
  TypeError: unsupported opperand type(s) for %: 'int' and 'list' cool_person 7 2,175 May-07-2022, 08:40 AM
Last Post: ibreeden
  unsupported operand type(s) for %: 'list' and 'int' RandomCoder 4 32,884 May-07-2022, 08:07 AM
Last Post: menator01
  You have any idea, how fix TypeError: unhashable type: 'list' lsepolis123 2 3,013 Jun-02-2021, 07:55 AM
Last Post: supuflounder
  What type of *data* is the name of a list/tuple/dict, etc? alloydog 9 4,395 Jan-30-2021, 07:11 AM
Last Post: alloydog
Question dict value, how to change type from int to list? swissjoker 3 2,758 Dec-09-2020, 09:50 AM
Last Post: perfringo
  TypeError: unhashable type: 'set' Stager 1 2,613 Jun-08-2020, 04:11 PM
Last Post: bowlofred
  List items verification for Integer type vintysaw 4 2,902 Jan-17-2020, 01:56 PM
Last Post: perfringo
  Type hinting - return type based on parameter micseydel 2 2,499 Jan-14-2020, 01:20 AM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020