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
814
votes
15 answers

How to define hash tables in Bash?

What is the equivalent of Python dictionaries but in Bash (should work across OS X and Linux).
Sridhar Ratnakumar
  • 81,433
  • 63
  • 146
  • 187
802
votes
13 answers

How do I convert a Map to List in Java?

How do I convert a Map to a List? Should I iterate over all map values and insert them into a list?
Javaa
  • 8,059
  • 3
  • 17
  • 5
798
votes
15 answers

Collections.defaultdict difference with normal dict

I've read the examples in python docs, but still can't figure out what this method means. Can somebody help? Here are two examples from the python docs >>> from collections import defaultdict >>> s = 'mississippi' >>> d = defaultdict(int) >>> for k…
Lanston
  • 11,354
  • 8
  • 32
  • 37
776
votes
9 answers

What is the difference between dict.items() and dict.iteritems() in Python2?

Are there any applicable differences between dict.items() and dict.iteritems()? From the Python docs: dict.items(): Return a copy of the dictionary’s list of (key, value) pairs. dict.iteritems(): Return an iterator over the dictionary’s (key,…
the wolf
  • 34,510
  • 13
  • 53
  • 71
771
votes
23 answers

Filter dict to contain only certain keys?

I've got a dict that has a whole bunch of entries. I'm only interested in a select few of them. Is there an easy way to prune all the other ones out?
mpen
  • 272,448
  • 266
  • 850
  • 1,236
767
votes
25 answers

Search a list of dictionaries in Python

Given: [ {"name": "Tom", "age": 10}, {"name": "Mark", "age": 5}, {"name": "Pam", "age": 7} ] How do I search by name == "Pam" to retrieve the corresponding dictionary below? {"name": "Pam", "age": 7}
Hellnar
  • 62,315
  • 79
  • 204
  • 279
750
votes
15 answers

Return a default value if a dictionary key is not available

I need a way to get a dictionary value if its key exists, or simply return None, if it does not. However, Python raises a KeyError exception if you search for a key that does not exist. I know that I can check for the key, but I am looking for…
Spyros
  • 46,820
  • 25
  • 86
  • 129
736
votes
6 answers

Are dictionaries ordered in Python 3.6+?

Dictionaries are insertion ordered as of Python 3.6. It is described as a CPython implementation detail rather than a language feature. The documentation states: dict() now uses a “compact” representation pioneered by PyPy. The memory usage of the…
Chris_Rands
  • 38,994
  • 14
  • 83
  • 119
721
votes
8 answers

Proper way to initialize a C# dictionary with values

I am creating a dictionary in a C# file with the following code: private readonly Dictionary FILE_TYPE_DICT = new Dictionary { {"csv", XlFileFormat.xlCSV}, {"html",…
azrosen92
  • 8,357
  • 4
  • 26
  • 45
694
votes
16 answers

Rename a dictionary key

Is there a way to rename a dictionary key, without reassigning its value to a new name and removing the old name key; and without iterating through dict key/value? In case of OrderedDict do the same, while keeping that key's position.
rabin utam
  • 13,930
  • 3
  • 20
  • 15
682
votes
10 answers

How can I use pickle to save a dict (or any other Python object)?

I have looked through the information that the Python documentation for pickle gives, but I'm still a little confused. What would be some sample code that would write a new file and then use pickle to dump a dictionary into it?
Chachmu
  • 7,725
  • 6
  • 30
  • 35
677
votes
3 answers

Add a new item to a dictionary in Python

How do I add an item to an existing dictionary in Python? For example, given: default_data = { 'item1': 1, 'item2': 2, } I want to add a new item such that: default_data = default_data + {'item3': 3}
brsbilgic
  • 11,613
  • 16
  • 64
  • 94
671
votes
8 answers

Python "extend" for a dictionary

What is the best way to extend a dictionary with another one while avoiding the use of a for loop? For instance: >>> a = { "a" : 1, "b" : 2 } >>> b = { "c" : 3, "d" : 4 } >>> a {'a': 1, 'b': 2} >>> b {'c': 3, 'd': 4} Result: { "a" : 1, "b" : 2, "c"…
FerranB
  • 35,683
  • 18
  • 66
  • 85
671
votes
11 answers

How to iterate (keys, values) in JavaScript?

I have a dictionary that has the format of dictionary = {0: {object}, 1:{object}, 2:{object}} How can I iterate through this dictionary by doing something like for ((key, value) in dictionary) { //Do stuff where key would be 0 and value would…
nbroeking
  • 8,360
  • 5
  • 20
  • 40
661
votes
45 answers

How to convert a nested Python dict to object?

I'm searching for an elegant way to get data using attribute access on a dict with some nested dicts and lists (i.e. javascript-style object syntax). For example: >>> d = {'a': 1, 'b': {'c': 2}, 'd': ["hi", {'foo': "bar"}]} Should be accessible in…
Marc
  • 6,741
  • 3
  • 19
  • 8