C={}
D={}

C['21600']=[{'vid':1,'route':[11,12,13,14,15]},{'vid':2,'route':[21,22,23,24,25]}]
C['23500']=[{'vid':3,'route':[31,32,33,34,35]},{'vid':4,'route':[41,42,43,44,45]}]

#D['21600']=[{'vid':1,'route':[11,12,13]},{'vid':2,'route':[23,24,25]}]
#D['23500']=[{'vid':3,'route':[31,32]},{'vid':4,'route':[45]}]

D=C
for n in D:
        for n2 in D[n]:
                if n2['vid']==1:
                        n2['route'].remove(14)
                        n2['route'].remove(15)
                elif n2['vid']==2:
                        n2['route'].remove(21)
                        n2['route'].remove(22)
                elif n2['vid']==3:
                        n2['route'].remove(33)
                        n2['route'].remove(34)
                        n2['route'].remove(35)
                elif n2['vid']==4:
                        n2['route'].remove(41)
                        n2['route'].remove(42)
                        n2['route'].remove(43)
                        n2['route'].remove(44)

print 'Printing D ', D



for u in D:
        for u2 in D[u]:
                print len(u2['route'])

print 'Printing C ',C

for ux in C:
        for ux2 in C[ux]:
                print len(ux2['route'])

Hi guys , When i display C and D they are the same . How come C is modified ?

Recommended Answers

All 3 Replies

Use copy.deepcopy to copy list and it's dictionaries.

Python uses pointers, which means that D=C assigns the memory address of "C" to "D" so they both point to the same block of memory.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.