Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Dictionary Keys and Values enumerated in the same order?

Tags:

c#

dictionary

I have scenario where I have to send the Dictionary that was sent as an input as comma seperated strings to a stored procedure.

I am wondering whether if I do it like below will there be any possible case that it may send the incorrect value for a given key in the dictionary?

static void Main(string[] args)
{
    Dictionary<int, string> test = new Dictionary<int, string>();
    test.Add(1, "1");
    test.Add(3, "3");
    test.Add(4, "4");
    test.Add(5, "5");
    test.Add(2, "2");
    JoinTest(test);
}

private static void JoinTest(Dictionary<int, string> test)
{
    var keys = string.Join(",", test.Keys);
    var values = string.Join(",", test.Values);
}
like image 664
Silly Volley Avatar asked Dec 16 '15 07:12

Silly Volley


People also ask

Are dict keys and dict values in the same order?

Yes. Starting with CPython 3.6, dictionaries return items in the order you inserted them.

Are dictionary values ordered?

As you'll see later in the tutorial, using a list of tuples could be the best choice for your data. An essential point to understand when sorting dictionaries is that even though they conserve insertion order, they're not considered a sequence. A dictionary is like a set of key-value pairs, and sets are unordered.

Are Keys in a dictionary ordered?

Answer. No, there is no guaranteed order for the list of keys returned by the keys() function. In most cases, the key list is returned in the same order as the insertion, however, that behavior is NOT guaranteed and should not be depended on by your program.

Is a dictionary always in order Python?

As of Python 3.6, for the CPython implementation of Python, dictionaries remember the order of items inserted. This is considered an implementation detail in Python 3.6; you need to use OrderedDict if you want insertion ordering that's guaranteed across other implementations of Python (and other ordered behavior).

What are the ordered keys and values of the Dictionary?

The original dictionary is : {'Gfg': 1, 'is': 3, 'Best': 2} The ordered keys and values : ['Gfg', 'is', 'Best', 1, 3, 2] This is one of the ways in which this task can be performed. In this, we bind keys with values together in order using chain ().

Are keys() and values() always the same in Python dictionary?

Python dictionary: are keys () and values () always the same order? It looks like the lists returned by keys () and values () methods of a dictionary are always a 1-to-1 mapping (assuming the dictionary is not altered between calling the 2 methods).

Do dictionaries return items in the order you inserted them?

If keys, values and items views are iterated over with no intervening modifications to the dictionary, the order of items will directly correspond. Yes. Starting with CPython 3.6, dictionaries return items in the order you inserted them. Ignore the part that says this is an implementation detail.

Is there a way to get dictionary keys in alphabetical order?

Kindly help if there is an easier way to either have dictionary consume the env keys exactly in the same order they are being fed/ingested from array i.e. key1, key2, key 3 or just can reverse it to have key3, key 2, key1. But don't want the existing way it is showing alphabetical order based on the actual key names.


2 Answers

Read the documentation. It clearly states:

The order of the keys in the Dictionary.KeyCollection is unspecified, but it is the same order as the associated values in the Dictionary.ValueCollection returned by the Values property.

and

The order of the values in the Dictionary.ValueCollection is unspecified, but it is the same order as the associated keys in the Dictionary.KeyCollection returned by the Keys property.

So yes, keys and values match, but as commenters pointed out, you may have other problems here already.

like image 128
Joey Avatar answered Nov 12 '22 01:11

Joey


Joey's answer is correct, although the order is only guaranteed if no modifications has been made to the dictionary, as pointed out in the comments. If you want to make sure that the order is the same you can do like this:

var dictionary = new Dictionary<int, string>();
var listOfKeyValuePairs = dictionary.ToList();
var keys = listOfKeyValuePairs.Select(kvp => kvp.Key);
var values = listOfKeyValuePairs.Select(kvp => kvp.Value);
like image 30
Fredrik Wallén Avatar answered Nov 11 '22 23:11

Fredrik Wallén