10

I have a list whose each element is a dictionary. Each element looks something like this

{'CELL': <Cell SOW16007.2.AC7>, 'COUNT': 2, 'NAMELIST': [], 'NAME': u'', 'LEVEL': u'SSE'}

I need to make a backup of this list.Normal assignment or using shallow copy is not option i can use as i will making changes to the original. But when i use deepcopy backUpNames=deepcopy(oldNames) I'm getting an error :

TypeError: unhashable type: 'array.array'

What wrong am i doing here? How can i solve this?

This is a not a duplicate question as I have already used deepcopy, problem i am facing is with the error deepcopy is throwing.

Minimal Code:

Using openpxl i iterate the sheet and append the values to a list

wb=openpyxl.load_workbook(sys.argv[3],data_only=True)
_ts=wb.active 
oldNames.append({'NAME':_ts['G7'].value,'LEVEL':_ts['H7'].value,'CELL':_ts['F7'],'COUNT':0,'NAMELIST':[]})
backUpNames=deepcopy(oldNames)#error occurring here

Thank You

8
  • Please provide a minimal reproducible example. Dec 16, 2016 at 6:13
  • @MoinuddinQuadri - That's not a duplicate at all. OP is using copy.deepcopy already, and getting an error. Dec 16, 2016 at 6:22
  • 1
    Won't this give syntax error? <Cell SOW16007.2.AC7> Dec 16, 2016 at 6:24
  • From given error, I can assume that Cell is some complex type that doesn't support hashing. Can You confirm by trying this: from collections import Hashable; isinstance(d['CELL'], Hashable)?
    – Fejs
    Dec 16, 2016 at 6:35
  • What is each there?
    – Fejs
    Dec 16, 2016 at 6:46

2 Answers 2

2
import copy
list = [{'a':1,'b':2},{'c':3,'d':4}]
cpy_list = []
for li in list:
    d2 = copy.deepcopy(li)
    cpy_list.append(d2)
print cpy_list
2
  • 1
    What's the point here? OP said he knows how to use deepcopy, he have a specific problem, for which this doesn't help a bit.
    – Fejs
    Dec 16, 2016 at 7:01
  • 1
    I still get the same error TypeError: unhashable type: 'array.array'.
    – Vinod Pn
    Dec 16, 2016 at 7:06
1

I think that this will fix the problem:

oldNames.append({'NAME':_ts['G7'].value,'LEVEL':_ts['H7'].value,'CELL':_ts['F7'].value,'COUNT':0,'NAMELIST':[]})

Note this key/value pair: 'CELL':_ts['F7'].value

I suppose that You can't use cell as object here, but rather only it's value.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.