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.


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
43 answers

How do I merge two dictionaries in a single expression in Python?

I want to merge two dictionaries into a new dictionary. x = {'a': 1, 'b': 2} y = {'b': 3, 'c': 4} z = merge(x, y) >>> z {'a': 1, 'b': 3, 'c': 4} Whenever a key k is present in both dictionaries, only the value y[k] should be kept.
Carl Meyer
  • 122,012
  • 20
  • 106
  • 116
17 answers

Iterating over dictionaries using 'for' loops

d = {'x': 1, 'y': 2, 'z': 3} for key in d: print(key, 'corresponds to', d[key]) How does Python recognize that it needs only to read the key from the dictionary? Is key a special keyword, or is it simply a variable?
  • 43,745
  • 10
  • 28
  • 27
45 answers

How do I efficiently iterate over each entry in a Java Map?

If I have an object implementing the Map interface in Java and I wish to iterate over every pair contained within it, what is the most efficient way of going through the map? Will the ordering of elements depend on the specific map implementation…
  • 38,281
  • 3
  • 21
  • 19
21 answers

How can I add new keys to a dictionary?

How do I add a new key to an existing dictionary? It doesn't have an .add() method.
  • 49,562
  • 17
  • 52
  • 70
34 answers

How do I sort a dictionary by value?

I have a dictionary of values read from two fields in a database: a string field and a numeric field. The string field is unique, so that is the key of the dictionary. I can sort on the keys, but how can I sort based on the values? Note: I have read…
Gern Blanston
  • 42,482
  • 19
  • 50
  • 64
20 answers

How to iterate over a dictionary?

I've seen a few different ways to iterate over a dictionary in C#. Is there a standard way?
Jake Stewart
  • 32,729
  • 3
  • 20
  • 17
11 answers

How can I remove a key from a Python dictionary?

I want to remove a key from a dictionary if it is present. I currently use this code: if key in my_dict: del my_dict[key] Without the if statement, the code will raise KeyError if the key is not present. How can I handle this more simply? See…
  • 36,591
  • 10
  • 48
  • 83
22 answers

How to sort a list of dictionaries by a value of the dictionary in Python?

How do I sort a list of dictionaries by a specific key's value? Given: [{'name': 'Homer', 'age': 39}, {'name': 'Bart', 'age': 10}] When sorted by name, it should become: [{'name': 'Bart', 'age': 10}, {'name': 'Homer', 'age': 39}]
  • 118
  • 3
  • 5
  • 9
16 answers

Check if a given key already exists in a dictionary

I wanted to test if a key exists in a dictionary before updating the value for the key. I wrote the following code: if 'key1' in dict.keys(): print "blah" else: print "boo" I think this is not the best way to accomplish this task. Is there a…
Mohan Gulati
  • 28,219
  • 5
  • 22
  • 20
15 answers

Determine the type of an object?

Is there a simple way to determine if a variable is a list, dictionary, or something else?
Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
19 answers

Delete an element from a dictionary

How do I delete an item from a dictionary in Python? Without modifying the original dictionary, how do I obtain another dict with the item removed? See also How can I remove a key from a Python dictionary? for the specific issue of removing an item…
  • 40,440
  • 14
  • 56
  • 86
65 answers

Sort a Map by values

I need to sort a Map on the values. Since the values are not unique, I find myself converting the keySet into an array, and sorting that array through array sort with a custom comparator that sorts on the value associated with the…
  • 2,463
  • 4
  • 19
  • 16
21 answers

How can I make a dictionary (dict) from separate lists of keys and values?

I want to combine these: keys = ['name', 'age', 'food'] values = ['Monty', 42, 'spam'] Into a single dictionary: {'name': 'Monty', 'age': 42, 'food': 'spam'}
  • 46,642
  • 28
  • 120
  • 174
22 answers

How to directly initialize a HashMap (in a literal way)?

Is there some way of initializing a Java HashMap like this?: Map test = new HashMap{"test":"test","test":"test"}; What would be the correct syntax? I have not found anything regarding this. Is this possible? I am…
  • 16,455
  • 4
  • 21
  • 20
17 answers

Create a dictionary with comprehension

Can I use list comprehension syntax to create a dictionary? For example, by iterating over pairs of keys and values: d = {... for k, v in zip(keys, values)}
  • 261,858
  • 191
  • 397
  • 503
2 3
99 100