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
25
votes
3 answers

AttributeError: 'str' object has no attribute 'items'

In the following code: #!/usr/local/bin/python import json APPLICATION_NAME = 'cc9226315643df89-36bf02429075329d0ba36748360d050c' HEADERS1 = json.dumps(dict(Destination = u"/api/af/latest/applications/%s/rulesets" % (APPLICATION_NAME))) print…
snrkl
  • 253
  • 1
  • 3
  • 4
25
votes
1 answer

When to use a HybridDictionary over other Dictionary types?

I am looking at the Collection classes in MSDN for the .Net framework. I ran into the HybridDictionary and it states (http://msdn.microsoft.com/en-us/library/system.collections.specialized.hybriddictionary.aspx): Implements IDictionary by using a…
Mike de Klerk
  • 11,906
  • 8
  • 54
  • 76
25
votes
3 answers

Group by and aggregate the values of a list of dictionaries in Python

I'm trying to write a function, in an elegant way, that will group a list of dictionaries and aggregate (sum) the values of like-keys. Example: my_dataset = [ { 'date': datetime.date(2013, 1, 1), 'id': 99, 'value1':…
Kyle Getrost
  • 707
  • 1
  • 5
  • 11
25
votes
4 answers

How to subtract values from dictionaries

I have two dictionaries in Python: d1 = {'a': 10, 'b': 9, 'c': 8, 'd': 7} d2 = {'a': 1, 'b': 2, 'c': 3, 'e': 2} I want to substract values between dictionaries d1-d2 and get the result: d3 = {'a': 9, 'b': 7, 'c': 5, 'd': 7 } Now I'm using two…
Colargol
  • 749
  • 4
  • 14
  • 26
25
votes
3 answers

JSON dumps custom formatting

I'd like to dump a Python dictionary into a JSON file with a particular custom format. For example, the following dictionary my_dict, 'text_lines': [{"line1"}, {"line2"}] dumped with f.write(json.dumps(my_dict, sort_keys=True, indent=2)) looks…
Saar Drimer
  • 1,171
  • 2
  • 11
  • 24
25
votes
2 answers

How to select a map pin programmatically

I am new to Maps Concept. Please find the attached image once. when i click the "pin" i am getting message says "Admire your smile" or any text..... Now i want like... when we select the table view, i need to raise that message for that pin(with…
Babul
  • 1,268
  • 2
  • 15
  • 42
25
votes
2 answers

Python 2.7 Counting number of dictionary items with given value

first question here, so i will get right to it: using python 2.7 I have a dictionary of items, the keys are an x,y coordinate represented as a tuple: (x,y) and all the values are Boolean values. I am trying to figure out a quick and clean method of…
jguerra
  • 303
  • 1
  • 4
  • 7
25
votes
4 answers

.NET: Are Dictionary values stored by reference or value

I have a Dictionary. If the same Product is added to more than one key is an new instance of that object stored for each key? Or just a reference to the original object? This collection is very large and each product will have…
NSjonas
  • 10,693
  • 9
  • 66
  • 92
25
votes
5 answers

Cocoa: Dictionary with enum keys?

I need to create a dictionary/hashmap where the Keys are enums Values are some subclass of NSObject NSDictionary won't work here (enums don't conform to NSCopying). I could perhaps use a CFDictionaryRef here, but I'd like to know if is there any…
Debajit
  • 46,327
  • 33
  • 91
  • 100
25
votes
2 answers

JPA - @OneToMany as a Map

This seems like a common enough case, but as JPA newbie, I am having trouble figuring this out. I'm using EclipseLink and PostgreSQL, but this should relate to just the JPA spec. I have one table PRIMARY that has an ID and then a bunch of other…
dnc253
  • 39,967
  • 41
  • 141
  • 157
25
votes
6 answers

How to write a getter and setter for a Dictionary?

How do you define a getter and setter for complex data types such as a dictionary? public Dictionary Users { get { return m_Users; } set { m_Users = value; } } This returns the entire…
Mausimo
  • 8,018
  • 12
  • 52
  • 70
25
votes
5 answers

Rationale for C++'s std map insert semantics?

I'm a little bit confused by std::map::insert's semantics. I mean, I'm not complaining - the standard is the standard and the API is the way it is. Still, insert will the insertion operation checks for each element inserted whether another…
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
25
votes
4 answers

How to sort dictionaries of objects by attribute value?

I would like to iterate over a dictionary of objects in an attribute sorted way import operator class Student: def __init__(self, name, grade, age): self.name = name self.grade = grade self.age = age studi1 =…
Fienchen21
  • 253
  • 1
  • 3
  • 4
24
votes
2 answers

Type Error: Format Requires Mapping

I have a string and a list of objects: gpl = "%(id)s : %(atr)s" objects = [{'id':1, 'content':[{'atr':'big', 'no':2}]}, {'id':2, 'content': [{'atr':'small', 'no':3}]}] for obj in objects: for con in obj['content']: print gpl…
Neeran
  • 1,753
  • 3
  • 21
  • 26
24
votes
3 answers

Time complexity of find() in std::map?

How efficient is the find() function on the std::map class? Does it iterate through all the elements looking for the key such that it's O(n), or is it in a balanced tree, or does it use a hash function or what?
Publius
  • 1,184
  • 2
  • 10
  • 27