473,387 Members | 3,684 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Dictionaries of Lists

I'd like to associate certain lists with keywords, and
retrieve them. But this is not possible as lists are
not hashable.

What is the best workaround? I don't mind making my
lists immutable. Is there a way to tupelize them?
I tried mydict[mykey]=([a for a in list]) but it
didn't seem to work.

Thanks!

PS If possible please cc me on all responses.

__________________________________
Celebrate Yahoo!'s 10th Birthday!
Yahoo! Netrospective: 100 Moments of the Web
http://birthday.yahoo.com/netrospective/
Jul 18 '05 #1
2 1002
gf gf wrote:
I'd like to associate certain lists with keywords, and
retrieve them. But this is not possible as lists are
not hashable.
You can convert them to tuples with the `tuple' function:

aDictionary[tuple(aList)] = aKeyword
What is the best workaround? I don't mind making my
lists immutable. Is there a way to tupelize them?
I tried mydict[mykey]=([a for a in list]) but it
didn't seem to work.


This code sample seems to suggest you're trying to do something else,
however. If `mykey' is a string, then what you wrote is completely
valid regardless of whether you converted it to a list or not.

If that was just a thinko while you were asking the question, then the
reason ([...]) didn't work is that it's just syntactically wrapping a
list comprehension in parentheses, rather than turning them into a tuple.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
Dead men have no victory.
-- Euripides
Jul 18 '05 #2
gf gf wrote:
I'd like to associate certain lists with keywords, and
retrieve them. But this is not possible as lists are
not hashable.
Do you want

mydict[mylist] = mykey

or

mydict[mykey] = mylist

?

The former is not possible with lists for the reason you noted. The
latter, however, works just fine. It is only the key that needs to be
hashable. The value can be any object.
What is the best workaround? I don't mind making my
lists immutable. Is there a way to tupelize them?
tuple(mylist)
I tried mydict[mykey]=([a for a in list]) but it
didn't seem to work.


--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Narendra C. Tulpule | last post by:
Hi, if you know the Python internals, here is a newbie question for you. If I have a list with 100 elements, each element being a long string, is it more efficient to maintain it as a dictionary...
7
by: Kerry Neilson | last post by:
Hi, Really hung up on this one. I'm trying to get all the fields of a dictionary to be unique for each class: class A { my_dict = dict_entry = { 'key1':0, 'key2':0 } __init__(self): for...
11
by: Amy G | last post by:
I have received such good help on this message board. I wonder if I might not get a little more help from you on this. I am at the point where I have two dictionaries, with information of a...
0
by: Till Plewe | last post by:
Is there a way to speed up killing python from within a python program? Sometimes shutting down takes more than 10 times as much time as the actual running of the program. The programs are...
210
by: Christoph Zwerschke | last post by:
This is probably a FAQ, but I dare to ask it nevertheless since I haven't found a satisfying answer yet: Why isn't there an "ordered dictionary" class at least in the standard list? Time and again...
2
by: David Pratt | last post by:
Hi. I like working with lists of dictionaries since order is preserved in a list when I want order and the dictionaries make it explicit what I have got inside them. I find this combination very...
5
by: Ladislav Andel | last post by:
Hi, I have a list of dictionaries. e.g. how could I make a new list of dictionaries which would look like: , 'service_domain': 'dp0.example.com'}, {'transports': , 'service_domain':...
3
by: ELK | last post by:
I recently started a beginners programming course and am struggling with my latest assignment. I have been given a .txt file of the results of a relay race with the following columns of data: team...
9
by: Brandon | last post by:
Hi all, I am not altogether experienced in Python, but I haven't been able to find a good example of the syntax that I'm looking for in any tutorial that I've seen. Hope somebody can point me...
14
by: cnb | last post by:
Are dictionaries the same as hashtables?
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.