grindelwaldus grindelwaldus - 1 year ago 100
Python Question

TypeError: unhashable type: 'list' from attempt to create dict

I'm receiving the following error in my programm:

Traceback (most recent call last):
File "", line 26, in <module>
zipping = dict(zip(datelist, matchhref))
TypeError: unhashable type: 'list'

I want to make dictionary from two lists (
), but somehow when I use
, it returns
instead of

Here's my code:

import re

bm_raw = open('bookmarks.txt', 'r')

bm_line =

matchhref = re.findall('(<DT><A HREF=".*?</A>)', bm_line)
massive = list()
datelist = list()
a = 0

for i in matchhref:

temp = matchhref[a]
found = re.findall('(\d\d\d\d\d\d\d\d\d\d)', temp)

print datelist
print matchhref
zipping = dict(zip(datelist, matchhref))

And here's contents of bookmarks.txt:

<DT><A HREF="some random data" ADD_DATE="1460617925" ICON="some random data">priomap</A>
<DT><A HREF="some random data" ADD_DATE="1455024833" ICON="some random data">V.34</A>

Answer Source

As I commented, you can call and then .group() the add the string and not the list that findall returns so you can use the string as the key but BeautifulSoup will make your life a lot easier:

In [50]:from bs4 import BeautifulSoup, Tag

In [51]: soup = BeautifulSoup(h,"xml")

In [52]: print(dict((dt["ADD_DATE"], dt["HREF"],) for dt in"DT A[HREF]")))
{u'1455024833': u'some random data', u'1460617925': u'some random data'}

select("DT A[HREF]") finds all the anchor tags i.e A inside a DT tag that have a HREF attribute.

The regex solution would be:

    found ='(\d\d\d\d\d\d\d\d\d\d)', temp)     

But use a html parser like bs4 or something similar.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download