Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the advantage of using a Two Item Tuple versus a Dictionary?

I have code where I return a list of IWebElements and their corresponding names? My understanding is that a tuple with two items is basically the same thing but the Dictionary uses hash mapping to relate the two values. What is the advantage of using a Two Item Tuple over a Dictionary or vice versa?

public Dictionary<IWebElement, string> SelectAllOptions(IWebDriver driver, ref DataObject masterData)
    {
        //Get the ID of the dropdown menu
        DatabaseRetrieval.GetObjectRepository(ref masterData);
        var strDropMenuId = masterData.DictObjectRepository["ID"];
        //Find the dropdown menu and pull all options into a list
        try
        {
            var dropMenu = new SelectElement(driver.FindElement(By.Id(strDropMenuId)));
            // TODO want to know how we want this list to return. 
            var options = dropMenu.Options as List<IWebElement>;
            if (options != null)
            {
                var values = options.ToDictionary(option => option, option => option.Text);
                return values;
            }
        }
like image 720
Jonathan Kittell Avatar asked Apr 28 '14 17:04

Jonathan Kittell


People also ask

Which is better tuple or dictionary?

A tuple can contain different values with different datatype while a dictionary can contain only one datatype value at a time. Tuples are particularly useful for returning multiple values from a function. A dictionary can be used as a model object.

What are the advantage of using tuple?

The advantages of tuples over the lists are as follows: Tuples are faster than lists. Tuples make the code safe from any accidental modification. If a data is needed in a program which is not supposed to be changed, then it is better to put it in 'tuples' than in 'list'.

What is the difference between a tuple and a dictionary?

A tuple is an ordered collection of data. A set is an unordered collection. A dictionary is an unordered collection of data that stores data in key-value pairs.

Why should you use a dict instead of a list or a tuple?

Dictionary is unordered collection. List and dictionary objects are mutable i.e. it is possible to add new item or delete and item from it. Tuple is an immutable object. Addition or deletion operations are not possible on tuple object.


4 Answers

As a general rule, you do not want to "pay" * for possibilities that your program does not need. For example, if your program is interested in retrieving and processing a sequence of pairs (also known as "two-member tuples") but it does not need to perform lookups from the first member of a tuple to the second, then providing a collection of pairs is more efficient:

IEnumerable<Tuple<IWebElement, string>> SelectAllOptions(...)
  • This approach takes less memory, because you do not allocate space for hash buckets
  • This approach takes less CPU, because there is no hash key computation or collision resolution costs
  • This approach does not suggest to a reader that the data is intended for lookups.

Of course if the data structure that you return is intended for lookups, then you should either return a dictionary, or construct one on the client side to transfer some of the CPU load from the server to the client.

* With memory, CPU cycles, decreased readability, etc.

like image 173
Sergey Kalinichenko Avatar answered Nov 15 '22 18:11

Sergey Kalinichenko


One advantage of tuples over dictionaries is that tuples can be named. Using List<(string text, string url)> links is more meaningful than Dictionary<string, string> links.

like image 44
Noxxys Avatar answered Nov 15 '22 17:11

Noxxys


A Tuple<T1, T2> represents a pair of values. That pair don't necessarily have to mean "These two items are related". When using a KeyValuePair<TKey, TValue>, you would expect that given a key, you would get a value and the two would have some sort of connection between one another.

Tuples implement IComparable and IStructuralEquatable, which makes it easier to compare Tuples. Other than that, I would look at it from a logical perspective, do I need to match a given key to a value?, or do I just need to couple together two values and a class might be a bit of an overhead for that.

One downside of Tuples as I see it, is that you have to deal with properties labeled Item1 and Item2, which might make it a bit less readable.

Also, remember that a Tuple is a class (an Immutable one) and KeyValuePair is a struct, so when you passing them as arguments you pass Tuple by reference and KeyValuePair by value (except for explicitly declaring ref or out)

like image 27
Yuval Itzchakov Avatar answered Nov 15 '22 17:11

Yuval Itzchakov


To add to the other answers, there are sometimes advantages to storing key-value data as a list of tuples instead of in a dictionary.

Depending on your needs, it might not be important that your lookups are fast, but it might be important that the order you insert into the list remains fixed. You can iterate through the keys and the values in a dictionary but the order is not defined.

Another advantage is that you can put as many pairs with the same first element into the list as you want, where with a dictionary you can only have one value per unique key.

like image 44
Luka Horvat Avatar answered Nov 15 '22 18:11

Luka Horvat