How to check a dictionary for duplicate values

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Agentfox

Hi, I’m working in Godot 3.1 and am somewhat new to Godot. For my project, I’m making a space RTS where there are planets that you can build ships on.
A key part of this game is that a fleet is a grouping of ships.
Every planet stores the ships docked in a dictionary and what fleets they belong to in a seperate dictionary. I want to have buttons for every ship that give you the fleet they belong to when you click on them. To do this I have 2 dicts, shipsDocked, and shipToFleetIndex, which uses the same keys as shipsDocked, but with fleets rather than ships.
For example: if shipsDocked is {1:corvette, 2:freighter, 3:freighter} and shipToFleetIndex is {1:fleet1, 2:fleet1, 3:fleet2} it means the corvette and freighter both belong to fleet1, but the other freighter belongs to fleet2.
Right now if I use this method to try and find the fleet (in this example), it would say both freighters belong to fleet1, because I used a for loop to find the key by the value, so it stops at the first freighter.

Is there any way to check for duplicates in a dictionary and then search the entire dictionary, rather than stopping at the first key?

I also would appreciate some insight into if there is a better way to do this than how I have it set up as I am a beginner.

Thanks in advance!

:bust_in_silhouette: Reply From: nationality

using the values() method puts all the dictionary into an array.

 
   array = dictionary.values()

You can then put them through a for loop. Although, you might consider making the ship the key, and their allegiance the value. That way, it’s impossible to have duplicates in the first place.

Once everything has been added into the array.
we can use array.count(element_name) for searching number of times the element got repeated in the array

ShashankLReddy | 2020-01-17 15:17