Questions tagged [dictionary]

A dictionary maps keys to values allowing efficient retrieval of values by keys. USE [map-function] TAG for mapping functions; and for geography, [maps].

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

63402 questions
5650
votes
49 answers

How do I merge two dictionaries in a single expression (taking union of dictionaries)?

I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged (i.e. taking the union). The update() method would be what I need, if it returned its result instead of modifying a dictionary…
Carl Meyer
  • 105,276
  • 18
  • 102
  • 113
3555
votes
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…
iMack
  • 34,445
  • 3
  • 19
  • 19
3520
votes
13 answers

Iterating over dictionaries using 'for' loops

I am a bit puzzled by the following code: d = {'x': 1, 'y': 2, 'z': 3} for key in d: print (key, 'corresponds to', d[key]) What I don't understand is the key portion. How does Python recognize that it needs only to read the key from the…
TopChef
  • 36,799
  • 10
  • 25
  • 27
3418
votes
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
  • 41,734
  • 19
  • 47
  • 64
3015
votes
19 answers

How can I add new keys to a dictionary?

Is it possible to add a key to a Python dictionary after it has been created? It doesn't seem to have an .add() method.
lfaraone
  • 44,680
  • 16
  • 48
  • 70
2856
votes
30 answers

What is the best way 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
  • 28,753
  • 3
  • 18
  • 14
2680
votes
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,129
  • 5
  • 20
  • 20
2202
votes
17 answers

How do I sort a list of dictionaries by a value of the dictionary?

I have a list of dictionaries and want each item to be sorted by a specific value. Take into consideration the list: [{'name':'Homer', 'age':39}, {'name':'Bart', 'age':10}] When sorted by name, it should become: [{'name':'Bart', 'age':10},…
masi
  • 118
  • 3
  • 5
  • 9
2201
votes
13 answers

How can I remove a key from a Python dictionary?

When deleting a key from a dictionary, I use: if 'key' in my_dict: del my_dict['key'] Is there a one line way of doing this?
Tony
  • 30,345
  • 9
  • 45
  • 77
1913
votes
14 answers

Determine the type of an object?

Is there a simple way to determine if a variable is a list, dictionary, or something else? I am getting an object back that may be either type and I need to be able to tell the difference.
Justin Ethier
  • 122,367
  • 49
  • 219
  • 273
1737
votes
59 answers

Sort a Map by values

I am relatively new to Java, and often find that 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…
Abe
  • 2,463
  • 4
  • 19
  • 16
1702
votes
15 answers

Delete an element from a dictionary

Is there a way to delete an item from a dictionary in Python? Additionally, how can I delete an item from a dictionary to return a copy (i.e., not modifying the original)?
richzilla
  • 34,719
  • 13
  • 51
  • 80
1436
votes
18 answers

How do I convert two lists into a dictionary?

Imagine that you have the following list. keys = ['name', 'age', 'food'] values = ['Monty', 42, 'spam'] What is the simplest way to produce the following dictionary? a_dict = {'name': 'Monty', 'age': 42, 'food': 'spam'}
Guido
  • 42,125
  • 24
  • 113
  • 167
1392
votes
14 answers

Create a dictionary with list comprehension

I like the Python list comprehension syntax. Can it be used to create dictionaries too? For example, by iterating over pairs of keys and values: mydict = {(k,v) for (k,v) in blah blah blah} # doesn't work
flybywire
  • 232,954
  • 184
  • 384
  • 491
1282
votes
20 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…
jens
  • 13,525
  • 4
  • 19
  • 20
1
2 3
99 100