Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a Tuple as a Key in a Dictionary C#

I have a Dictionary fieldTracker which takes a Tuple<int, int> as Key and string as value. However, I can't seem to find the right way to access the value. Here is my current code:

for (int i = 0; i < 2; i++)
  {
    for (int j = 0; j < 5; j++)
      dict.Add(new Tuple<int, int>(i, j), "");
  }
  dict[(1,1)] = "Hello";

I've searched around a bit in the Microsoft documentation, but can't find the key to this problem.

like image 918
JavaHR Avatar asked Sep 07 '18 10:09

JavaHR


People also ask

Can I use tuple as key in dictionary?

A tuple containing a list cannot be used as a key in a dictionary. Answer: True. A list is mutable. Therefore, a tuple containing a list cannot be used as a key in a dictionary.

Can a tuple be a dictionary key C#?

NET 4's Tuple implements equals so it can be used in a dictionary. Your GetHashCode implementation isn't very good. It's invariant under permutation of the fields. Tuple should not be a struct.

How do you add a tuple to a dictionary?

When it is required to add a dictionary to a tuple, the 'list' method, the 'append', and the 'tuple' method can be used. A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on). The 'append' method adds elements to the end of the list.

How are dictionaries and tuples used together?

Because tuples are hashable and lists are not, if we want to create a composite key to use in a dictionary we must use a tuple as the key. Write code to create a dictionary called 'd1', and in it give the tuple (1, 'a') a value of “tuple”.

How to get a tuple as a key from a Python dictionary?

In Python the dict () method takes a list of tuples as an argument and each tuple store key-value pair element and it returns into a dictionary. Here is the Output of the following given code Let us see how to get a tuple as a key from a Python dictionary.

What is the difference between a tuple and a dictionary?

Therefore, the new element in the dictionary is not positioned at the end of the dictionary.) At this point, the keys in the dictionary consist of one number, one string, and one tuple. The tuple contains only immutable elements. Similarly, the values in the dictionary consist of one tuple and two strings.

What is a tuple in machine learning?

And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course Here tuple is a collection of elements that work as a key for some value Example: Python program to create a dictionary of tuples with a tuple as keys Here tuple is a collection of elements that represent some value for some key.

What is a tuple in C #?

C# tuple is a data structure in C#. Learn how to create a tuple in C#. Often, we want to return more than one value from a class method. Prior to the introduction of tuples in .NET, there were three common ways to do so. Tuples solve this problem. Tuples aren’t new to C# or .NET. Tuples were first introduced as a part of .NET Framework 4.0.


2 Answers

You can try this way.

        var dict = new Dictionary<Tuple<int, int>, string>();
        for (int i = 0; i < 2; i++)
        {
            for (int j = 0; j < 5; j++)
                dict.Add(Tuple.Create<int, int>(i, j), "Hello");
        }
        string val = dict[Tuple.Create<int, int>(1,1)];

Hope this helps :)

like image 130
Jawand Singh Avatar answered Oct 19 '22 17:10

Jawand Singh


dict[Tuple.Create(1, 1)] = "Hello";

or with C#7 ValueTuple:

var dict = new Dictionary<(int, int), string>();
for (int i = 0; i < 2; i++)
{
    for (int j = 0; j < 5; j++)
        dict.Add((i, j), "");
}
dict[(1, 1)] = "Hello";
like image 21
Tim Schmelter Avatar answered Oct 19 '22 17:10

Tim Schmelter