1

I am converting lists to dictionary...the dictionary may contain duplicate key value pairs. Python Dictionary returns key value pairs based on the last occurrence in the input data. How can i take first key-value pairs from the dictionary when a key repeats multiple times?

i see one solution as mentioned in following link, i need to create list of values for key and then take first element from the values list. List of values for duplicate keys in dictionary Python

Is there any other better way to do this?

Community
  • 1
  • 1
user491
  • 175
  • 1
  • 4
  • 20
  • 1
    The question does not seem to be clear. From what we know python dictionaries cannot contain duplicate keys ref https://docs.python.org/2/tutorial/datastructures.html#dictionaries – sid-m May 04 '17 at 05:52

1 Answers1

3

You can just iterate through your list backwards using reversed when constructing the dictionary. The constructed dictionary will then only contain the first values of the original list.

Karin
  • 8,404
  • 25
  • 34