Missing values in a dictionary?

1373
4
Jump to solution
05-29-2020 02:32 AM
JohannesBierer
Occasional Contributor III

Somehow I'm not able to add all values in a search cursor to a dictionary - one value pair is missing? What is my mistake?

def Dict1(self):

        with arcpy.da.SearchCursor(outIntersect, ["FID_Anbinden1", "FLURSTUECKSTEXT"]) as cursor:
            for row in cursor:
                print("{} - {}".format(row[0], row[1]))
                myKey = row[1]
                myValue = row[0]
                myDict[myKey] = myValue
                # myDict.update({myKey: myValue})

        print myDict

This leads to this output (as well as .update in line 9):

1 - 10125
1 - 10079
1 - 10337/2
2 - 10125
{u'10079': 1, u'10337/2': 1, u'10125': 2}

u'10125':1 is missing?

0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

Your keys have to be unique. The key: value pair  "10125: 1" is getting overwritten by the pair "10125: 2"

View solution in original post

0 Kudos
4 Replies
KenBuja
MVP Esteemed Contributor

Your keys have to be unique. The key: value pair  "10125: 1" is getting overwritten by the pair "10125: 2"

0 Kudos
JohannesBierer
Occasional Contributor III

Thank you ...

0 Kudos
KenBuja
MVP Esteemed Contributor

If you need to have multiple values for each key, then use a list rather than a value, like "10125: [1,2]"

See this post: python - Adding new key to dictionary overwrites all previously stored keys with new keys values - S... 

0 Kudos
JohannesBierer
Occasional Contributor III

Ah ok, will try to use a list.

0 Kudos