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

Vinod Pn
  • 266
  • 1
  • 2
  • 10

2 Answers2

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
Jordfräs
  • 8,903
  • 3
  • 27
  • 30
dsingh
  • 29
  • 3
  • 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 '16 at 07:01
  • 1
    I still get the same error TypeError: unhashable type: 'array.array'. – Vinod Pn Dec 16 '16 at 07: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.

Fejs
  • 2,734
  • 3
  • 21
  • 40