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
660
votes
8 answers

How to check if a dictionary is empty?

I am trying to check if a dictionary is empty but it doesn't behave properly. It just skips it and displays ONLINE without anything aside from the display the message. Any ideas why ? def isEmpty(self, dictionary): for element in dictionary: …
Unsparing
  • 6,985
  • 2
  • 17
  • 33
648
votes
19 answers

What's the difference between ConcurrentHashMap and Collections.synchronizedMap(Map)?

I have a Map which is to be modified by several threads concurrently. There seem to be three different synchronized Map implementations in the Java API: Hashtable Collections.synchronizedMap(Map) ConcurrentHashMap From what I understand,…
Henning
  • 11,496
  • 5
  • 27
  • 25
640
votes
10 answers

How to update the value stored in Dictionary in C#?

How to update value for a specific key in a dictionary Dictionary?
Amit
  • 25,106
  • 25
  • 75
  • 116
634
votes
29 answers

Merging dictionaries in C#

What's the best way to merge 2 or more dictionaries (Dictionary) in C#? (3.0 features like LINQ are fine). I'm thinking of a method signature along the lines of: public static Dictionary
orip
  • 73,323
  • 21
  • 116
  • 148
621
votes
15 answers

How to find if a given key exists in a C++ std::map

I'm trying to check if a given key is in a map and somewhat can't do it: typedef map::iterator mi; map m; m.insert(make_pair("f","++--")); pair p = m.equal_range("f");//I'm not sure if equal_range does what I…
There is nothing we can do
  • 23,727
  • 30
  • 106
  • 194
618
votes
17 answers

Map vs Object in JavaScript

I just discovered this feature: Map: Map objects are simple key/value maps. That confused me. Regular JavaScript objects are dictionaries, so how is a Map different from a dictionary? Conceptually, they're identical (according to another question…
Dave
  • 44,275
  • 12
  • 65
  • 105
615
votes
7 answers

Error: " 'dict' object has no attribute 'iteritems' "

I'm trying to use NetworkX to read a Shapefile and use the function write_shp() to generate the Shapefiles that will contain the nodes and edges, but when I try to run the code it gives me the following error: Traceback (most recent call last): …
friveraa
  • 6,265
  • 2
  • 12
  • 7
611
votes
7 answers

How can I get list of values from dict?

How can I get a list of the values in a dict in Python? In Java, getting the values of a Map as a List is as easy as doing list = map.values();. I'm wondering if there is a similarly simple way in Python to get a list of values from a dict.
Muhd
  • 24,305
  • 22
  • 61
  • 78
607
votes
11 answers

How to do associative array/hashing in JavaScript

I need to store some statistics using JavaScript in a way like I'd do it in C#: Dictionary statistics; statistics["Foo"] = 10; statistics["Goo"] = statistics["Goo"] + 1; statistics.Add("Zoo", 1); Is there an Hashtable or something…
George2
  • 44,761
  • 110
  • 317
  • 455
596
votes
8 answers

Java Class that implements Map and keeps insertion order?

I'm looking for a class in java that has key-value association, but without using hashes. Here is what I'm currently doing: Add values to a Hashtable. Get an iterator for the Hashtable.entrySet(). Iterate through all values and: Get a Map.Entry…
Shane
  • 6,045
  • 3
  • 19
  • 7
594
votes
25 answers

Change the name of a key in dictionary

How do I change the key of an entry in a Python dictionary?
user469652
  • 48,855
  • 59
  • 128
  • 165
591
votes
14 answers

Access an arbitrary element in a dictionary in Python

If a mydict is not empty, I access an arbitrary element as: mydict[mydict.keys()[0]] Is there any better way to do this?
Stan
  • 37,207
  • 50
  • 124
  • 185
573
votes
12 answers

Remap values in pandas column with a dict, preserve NaNs

I have a dictionary which looks like this: di = {1: "A", 2: "B"} I would like to apply it to the col1 column of a dataframe similar to: col1 col2 0 w a 1 1 2 2 2 NaN to get: col1 col2 0 w a 1 …
TheChymera
  • 17,004
  • 14
  • 56
  • 86
557
votes
19 answers

How can I deserialize JSON with C#?

I have the following code: var user = (Dictionary)serializer.DeserializeObject(responsecontent); The input in responsecontent is JSON, but it is not properly deserialized into an object. How should I properly deserialize it?
user605334
551
votes
7 answers

Append a dictionary to a dictionary

I have two existing dictionaries, and I wish to 'append' one of them to the other. By that I mean that the key,values of the other dictionary should be made into the first dictionary. For example: orig = { 'A': 1, 'B': 2, 'C': 3, } extra =…
Javier Novoa C.
  • 11,257
  • 13
  • 57
  • 75