Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert dictionary values into array

What is the most efficient way of turning the list of values of a dictionary into an array?

For example, if I have a Dictionary where Key is String and Value is Foo, I want to get Foo[]

I am using VS 2005, C# 2.0

like image 525
leora Avatar asked Oct 13 '08 09:10

leora


People also ask

How do you convert a dictionary value to an array?

To convert a dictionary to an array in Python, use the numpy. array() method, and pass the dictionary object to the np. array() method as an argument and it returns the array.

Can you put a dictionary in an array?

You cannot use string indexes in arrays, but you can apply a Dictionary object in its place, and use string keys to access the dictionary items. The dictionary object has the following benefits when compared with arrays: The size of the Dictionary object can be set dynamically.

Can you make an array of dictionaries in Python?

In Python, we can have a list or an array of dictionaries. In such an object, every element of a list is a dictionary. Every dictionary can be accessed using its index.


1 Answers

// dict is Dictionary<string, Foo>  Foo[] foos = new Foo[dict.Count]; dict.Values.CopyTo(foos, 0);  // or in C# 3.0: var foos = dict.Values.ToArray(); 
like image 193
Matt Hamilton Avatar answered Sep 19 '22 23:09

Matt Hamilton