Questions tagged [dictionary]

A dictionary maps keys to values allowing efficient retrieval of values by keys. Use the [map-function] tag for mapping functions; use the [maps] tag for geography.

A dictionary (also known as map, associative array, or symbol table) in computer science is a data structure that maps keys to values such that given a key its corresponding value can be efficiently retrieved.

It is commonly implemented as a hash map which allow O(1) amortized lookup. Alternatively, they may be implemented as a sorted list, with lookup requiring a binary search and making the lookup O(log N) amortized instead.

When tagging a question with , be sure to tag it with the language being used as well.

In C++

std::map<Key, T, Compare, Allocator>: A sorted associative container that contains key-value pairs with unique keys.

In .NET

Dictionary<TKey, TValue>: Represents a collection of keys and values.

In Python

dict: Maps hashable keys to arbitrary objects.

In Java

Map<K, V>: An object that maps keys to values.

See also

Related tags: , , , , , ,


For questions about Mapping Functions over collections of data, use tag.

For questions about the Geographical maps i.e. for visual representation of area, please use tag

85558 questions
24
votes
6 answers

Is there such a thing as bidirectional maps in Scala?

I'd like to link 2 columns of unique identifiers and be able to get a first column value by a second column value as well as a second column value by a first column value. Something like Map(1 <-> "one", 2 <-> "two", 3 <-> "three") Is there such a…
Ivan
  • 63,011
  • 101
  • 250
  • 382
24
votes
2 answers

Why is the hash table of HashMap marked as transient although the class is serializable

I was looking at the source of HashMap. A HashMap implements Serializable. Ok this is so that it can be peristed/transmitted as an object. But I see that the hashtable itself is marked as transient. I don't get this.If you mark it as…
Cratylus
  • 52,998
  • 69
  • 209
  • 339
24
votes
3 answers

LINQ: Getting Keys for a given list of Values from Dictionary and vice versa

I have the following structure in my code Dictionary data;. I run some LINQ queries on both data types and often need to switch between Keys and Values. What is the best way to get the list of Keys for given Values and vice…
Alexander Galkin
  • 12,086
  • 12
  • 63
  • 115
24
votes
7 answers

Add new column to dataframe based on dictionary

I have a dataframe and a dictionary. I need to add a new column to the dataframe and calculate its values based on the dictionary. Machine learning, adding new feature based on some table: score = {(1, 45, 1, 1) : 4, (0, 1, 2, 1) : 5} df =…
Roman Kazmin
  • 931
  • 6
  • 18
24
votes
4 answers

C++ shared_ptr equality operator

The equality operator for shared_ptr's is defined as follows: template inline bool operator==( shared_ptr const & a, shared_ptr const & b) { return a.get() == b.get(); } This seems broken. Would it not have been…
user231536
  • 2,661
  • 4
  • 33
  • 45
24
votes
1 answer

How to get word vectors from Keras Embedding Layer

I'm currently working with a Keras model which has a embedding layer as first layer. In order to visualize the relationships and similarity of words between each other I need a function that returns the mapping of words and vectors of every element…
philszalay
  • 423
  • 1
  • 3
  • 9
24
votes
4 answers

Are IEEE floats valid key types for std::map and std::set?

Background The requirement for a comparator on the key type of an associative container (for example std::map) is that it imposes a strict weak order on the elements of the key type. For a given comparator comp(x, y) we define equiv(x, y) = !comp(x,…
etarion
  • 16,935
  • 4
  • 43
  • 66
24
votes
6 answers

NavigableMap vs. SortedMap?

Is there any reason to use SortedMap instead of NavigableMap, besides the JVM version? (NavigableMap has only been there since 1.6; SortedMap has been there since 1.2) I'm trying to find the value with the greatest key such that key <= reference key…
Jason S
  • 184,598
  • 164
  • 608
  • 970
24
votes
3 answers

How to get memory size of variable in Go?

I am curious about the memory cost of map and slice, so I wrote a program to compare the sizes. I get the memory size by unsafe.Sizeof(s), but obviously it is wrong, because when I change the size, the output is the same. func getSlice(size int)…
roger
  • 9,063
  • 20
  • 72
  • 119
24
votes
3 answers

How to add dictionaries to a DataFrame as a row?

I have a DataFrame with following columns: columns = ['Autor', 'Preţul', 'Suprafaţa totală', 'Etaj', 'Etaje', 'Tipul casei', 'Tipul de camere','Numărul de camere','Starea apartamentului', 'Planificare', 'Tipul clădirii', 'Sectorul', 'Strada', …
Sinchetru
  • 539
  • 1
  • 3
  • 13
24
votes
5 answers

Splitting a list of dictionaries into several lists of dictionaries

I've been whacking away at this for a while to no avail... Any help would be greatly appreciated. I have: [{'event': 0, 'voltage': 1, 'time': 0}, {'event': 0, 'voltage': 2, 'time': 1}, {'event': 1, 'voltage': 1, 'time': 2}, {'event': 1, 'voltage':…
thenickname
  • 6,684
  • 14
  • 41
  • 42
24
votes
5 answers

Arrays.asList also for maps?

I have the below code: Map> map = new HashMap<>(); Map Amap = new HashMap<>(); map.put(getValuesTypes.FUT(), HERE); Instead of creating a Map first and put it at "HERE", I'm looking for a function like I…
PowerFlower
  • 1,619
  • 4
  • 18
  • 27
24
votes
5 answers

C# Merging 2 dictionaries

I'm developing an app in C# targeting .NET 3.5. In it, I have 2 similar dictionaries that contain validation criteria for a specific set of elements in my app. Both dictionaries have identical signatures. The first dictionary has the default…
psubsee2003
  • 8,563
  • 8
  • 61
  • 79
24
votes
4 answers

C#: What does the [string] indexer of Dictionary return?

What does the [string] indexer of Dictionary return when the key doesn't exist in the Dictionary? I am new to C# and I can't seem to find a reference as good as the Javadocs. Do I get null, or do I get an exception?
jjnguy
  • 136,852
  • 53
  • 295
  • 323
24
votes
4 answers

Combinations from dictionary with list values using Python

I have the following incoming value: variants = { "debug" : ["on", "off"], "locale" : ["de_DE", "en_US", "fr_FR"], ... } I want to process them so I get the following result: combinations = [ [{"debug":"on"},{"locale":"de_DE"}], …
1 2 3
99
100