-2

I have a list in that list dictionary is present how to replace key of that dictionary?

a = [{ 1:'1',2:'2',3:'3',4:'4',5:'5',1:'1'}]
for n, i in enumerate(a):
if i == 1:
   a[n] = 10

1 is a key have to replace with 10,so i have tried in above method but can't able to do the final thing i want is

a = [{ 10:'1',2:'2',3:'3',4:'4',5:'5',10:'1'}]
Abduraoof
  • 17
  • 2
  • can you show what you'd have wanted the end result dictionary to look like? – Paritosh Singh Jul 13 '19 at 19:15
  • 1
    You cannot have duplicate keys in the dictionary, like you have here – Devesh Kumar Singh Jul 13 '19 at 19:16
  • @DeveshKumarSingh no it doesn't. The OP wants to replace the _key_ not the value (I think they edited that in after you commented, actually) – roganjosh Jul 13 '19 at 19:19
  • 2
    Your expected output is _impossible_. You cannot have a dictionary with two keys that are both `10`. That said, the original is just as impossible for the same reason. It is a requirement that dictionaries have unique keys, so any entries with the key will just be overwritten. In this particular example, it's not an issue because both `1` or `10` keys have the same value stored against them – roganjosh Jul 13 '19 at 19:20
  • 2
    Possible duplicate of [Change the name of a key in dictionary](https://stackoverflow.com/questions/4406501/change-the-name-of-a-key-in-dictionary) – Paritosh Singh Jul 13 '19 at 19:28
  • Welcome to SO. Please take the time to read [ask] and the other links found on that page. – wwii Jul 13 '19 at 19:45

2 Answers2

1

Original:

   a = [{ 1:1,2:2,3:3,4:4,5:5,1:1}]
   for n, i in enumerate(a):
       if i == 1:
          a[n] = 10

First thing to know is, arrays start at 0. You have

if i == 1:

and so, your code will never execute.

You also have a duplicate key in your dictionary - 1 is used twice.

Since your dictionary is in a list, it will have to be index like:

a[i][j] = ...

where i refers to which element it is in the list, and j refers to which element in the dictionary.

Last, your i and n are reversed - enumerate puts the index in the first variable.

So, if I understand correctly what you want to accomplish, the end result should end up looking more like this:

a = [{1:1,2:2,3:3,4:4,5:5}]
for i, n in enumerate(a):
    if i == 0:
        a[0][1] = 10

print(a)

If you want to change the value for more than 1 key, then I might do something like this:

a = [{1:1,2:2,3:3,4:4,5:5}]
toChange = [[1,10], [4, 76]] # 1 and 4 are the keys, and 10 and 76 are the values to change them to
for i, n in enumerate(a):
    if i == 0:
        for change in toChange:
            a[0][change[0]] = change[1]

print(a)

EDIT: Everything above is still correct, but as you and Tomerikoo pointed out, it does not quite answer the question. My apologies. The following code should work.

a = [{1: 1, 2: 2, 3: 3, 4: 4, 5: 5}]
toChange = [[1, 10], [4, 76]]  # 1 and 4 are the keys, and 10 and 76 are the 
values to change them to
for i, n in enumerate(a):
    if i == 0:
        for change in toChange:
            try:
                oldValue = a[0][change[0]]
                del a[0][change[0]]
                a[0][change[1]] = oldValue
            except:
                pass # handle it here
                #This likely means you tried to replace a key that isn't in there
print(a)
Axle12693
  • 46
  • 6
0

I wouldn't use enumerate here. Notice that the first key's value get's overwritten.

a = [{1: '1', 2: '2', 3: '3', 4: '4', 5: '5', 1: '1'}]  # duplicate key
print(a)            # [{1: '1', 2: '2', 3: '3', 4: '4', 5: '5'}]  (got overwritten)
a[0][10] = a[0][1]  # copy the value of the key 1 to the key 10
a[0].pop(1)         # remove the key 1
print(a)            # [{2: '2', 3: '3', 4: '4', 5: '5', 10: '1'}]

Also, note that in the original example the indentation of the if block is wrong.

ellockie
  • 3,730
  • 6
  • 42
  • 44