Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go equivalent of Python's Dictionary

Tags:

go

I am looking for a way to store multiple values for each key (just like we can in python using dictionary) using Go. Is there a way this can be achieved in Go?

like image 581
Parag Avatar asked Oct 26 '16 04:10

Parag


People also ask

Are there dictionaries in go?

Go provides a very convenient implementation of a dictionary by its built-in map type.

What can I use instead of a dictionary in python?

defaultdict. defaultdict handles our previous error by building on top of dictionaries. It does not give a KeyError like the regular dictionaries. However, whenever you try to access or “assign to” a key that is not there, it will create that key and give it a default value that was already specified .

Is Python dictionary order guaranteed?

Dictionary iteration order happens to be in order of insertion in CPython implementation, but it is not a documented guarantee of the language.

Can dictionary have same values in Python?

No, each key in a dictionary should be unique. You can't have two keys with the same value. Attempting to use the same key again will just overwrite the previous value stored. If a key needs to store multiple values, then the value associated with the key should be a list or another dictionary.


2 Answers

Based on your response in comments I would suggest something like the following using a struct (though if you are only interested in a single value like name for each item in your slice then you could just use a map[int][]string{}

type Thing struct {
    name string
    age  int
}

myMap := map[int][]Thing{}

If you want to add things then you just do...

myMap[100] = append(myMap[100], Thing{"sberry": 37})

Or if you want to create it in place:

myMap := map[int][]Thing{
        100: []Thing{Thing{"sberry", 37}},
        2:   []Thing{Thing{"johndoe", 22}},
    }

EDIT: Adding a "nameIn" function based on comments as demo:

func nameIn(things []Thing, name string) bool {
    for _, thing := range things {
        if thing.name == name {
            return true
        }
    }
    return false
}

if nameIn(myMap[100], "John") {
    ...

If the slices are really big and speed is concern then you could keep a reverse lookup map like map[string]int where an entry would be John: 100, but you will need to most likely use some user defined functions to do your map modifications so it can change the reverse lookup as well. This also limits you by requiring unique names.

Most likely the nameIn would work just fine.

like image 68
sberry Avatar answered Oct 13 '22 01:10

sberry


In go, the key/value collection is called a map. You create it with myMap := map[keyType]valType{}

Usually something like mapA := map[string]int{}. If you want to store multiple values per key, perhaps something like:

mapB := map[string][]string{} where each element is itself a slice of strings. You can then add members with something like:

mapB["foo"] = append(mapB["foo"], "fizzbuzz")

For a great read see Go maps in action

like image 21
captncraig Avatar answered Oct 13 '22 00:10

captncraig