Questions tagged [dictionary]
62 questions
99
votes
5 answers
Why store a function inside a python dictionary?
I'm a python beginner, and I just learned a technique involving dictionaries and functions. The syntax is easy and it seems like a trivial thing, but my python senses are tingling. Something tells me this is a deep and very pythonic concept and I'm…
mdeutschmtl
- 1,109
- 1
- 8
- 6
53
votes
4 answers
What is the difference between a hash and a dictionary?
What is the difference between Hash and Dictionary?
Coming from a scripting background, I feel that they are similar, but I wanted to find out the exact differences. Googling did not help me much.
Sairam
- 641
35
votes
9 answers
Is there a better way to use C# dictionaries than TryGetValue?
I find myself often looking up questions online, and many solutions include dictionaries. However, whenever I try to implement them, I get this horrible reek in my code. For example every time I want to use a value:
int x;
if…
Adam B
- 1,660
32
votes
5 answers
When to use a dictionary vs tuple in Python
The specific example in mind is a list of filenames and their sizes. I can't decide whether each item in the list should be of the form {"filename": "blabla", "size": 123}, or just ("blabla", 123). A dictionary seems more logical to me because to…
clb
- 521
32
votes
3 answers
Why should I use namedtuple over SimpleNamespace when not using dict, they seem very similar
At one point or another you might come over functions with a lot of arguments. Sometimes it makes sense to combine some of the arguments into super-arguments. I've often done this with dicts, but now I'm looking at better ways of doing it.
I'd like…
André Christoffer Andersen
- 1,455
- 2
- 13
- 18
27
votes
1 answer
Efficiency of C# dictionaries
C# dictionaries are a simple way to find if something exists etc etc. I have a question though on how they work. Let's say instead of a dictionary I use an ArrayList. Instead of using ContainsKey (or an equivalent method in another language) I loop…
John Demetriou
- 692
17
votes
5 answers
Efficient methods for storing tens of millions of objects for querying, with a high number of inserts per second?
This is basically a logging/counting application that is counting the number of packets and counting the type of packet, etc. on a p2p chat network. This equates to about 4-6 million packets in a 5 minute period. And because I only take a "snapshot"…
Josh
- 273
11
votes
4 answers
Practical size limits of a Hashtable and Dictionary in C#
What are the practical limits for the number of items a C# 4 Dictionary or Hashtable can contain and the total number of bytes these structures can reasonable contain?
I'll be working with large numbers of objects and want to know when these…
JoeGeeky
- 811
10
votes
3 answers
What are the advantages of linear probing over separate chaining or vice-versa when implementing hash tables?
I've been brushing up on algorithms and reviewed these two methods of implementing hash tables. It seems like they largely have similar performance characteristics and memory requirements.
I can think of some disadvantages to linear probing --…
Casey
- 273
8
votes
3 answers
Pointers vs keeping indices of objects stored in a central (associative) array?
Until recently I used to think that it was preferred to reference objects by pointers or references than to keep objects in some sort of a central, authoritative array or dictionary and only keep indices or keys of members of such an array.
However,…
gaazkam
- 4,529
7
votes
1 answer
Why are there two ways to retrieve values from a dictionary in Python?
In Python there are basically two ways to get a value from a dictionary:
dictionary["key"]
dictionary.get("key")
Is there any - maybe historical - reason for this behavior?
I sometimes stumble upon this while switching from JavaScript frontend…
Thomas Junk
- 9,623
- 2
- 26
- 46
7
votes
1 answer
Dictionary of dictionaries design in C#
My question: is there a canonical way of creating a dictionary of dictionaries and providing an outer/inner key pair? Is there a NugetPackage out there with an implementation?
In my code, I have now a few places where I have a property like…
Frank Bryce
- 968
7
votes
2 answers
In Python, is there any difference (apart from a few listed exceptions) between classes and dictionaries?
My logic goes like this:
def A(val):
return {'a':val}
print(A(7)['a'])
is the same as
class A:
def __init__(self, val):
self.a = val
print(A(7).a)
Obviously, there are problems and some differences:
This only applies to…
user3808430
- 173
6
votes
2 answers
Should I use a Class or Dictionary to Store Form Values
I am working on a C# .NET Application, where I have a Form with lots of controls. I need to perform computations depending on the values of the controls. Therefore, I need to pass the Form values to a function and inside that function, several…
Shamim Hafiz - MSFT
- 4,155
6
votes
2 answers
Why is there no key specified with .NET KeyNotFoundException?
Why is there no key specified with .NET KeyNotFoundException (in message/property)? Is it because of performance reasons? Is it because when Dictionary finds out there isnt any requested object by the hash it already doesnt know the context of the…