Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List with multiple values per key

Tags:

c#

.net

list

how do we create a list with multiple values for example , list[0] contains three values {"12","String","someValue"} the Some value is associated to the two other values i want to use a list rather than using an array

string[, ,] read = new string[3, 3, 3];
like image 202
marcAntoine Avatar asked Jun 27 '13 11:06

marcAntoine


People also ask

Can one key have multiple values?

Each key can only have one value. But the same value can occur more than once inside a Hash, while each key can occur only once.

Can you have multiple values per key Python?

In python, if we want a dictionary in which one key has multiple values, then we need to associate an object with each key as value. This value object should be capable of having various values inside it. We can either use a tuple or a list as a value in the dictionary to associate multiple values with a key.

How do I assign multiple values to a key in Python?

General Idea: In Python, if we want a dictionary to have multiple values for a single key, we need to store these values in their own container within the dictionary. To do so, we need to use a container as a value and add our multiple values to that container. Common containers are lists, tuples, and sets.

Can list have key value pairs?

A value in the key-value pair can be a number, a string, a list, a tuple, or even another dictionary. In fact, you can use a value of any valid type in Python as the value in the key-value pair. A key in the key-value pair must be immutable.


2 Answers

Why not have a List of Tuple's? It can be unclear, but it will do what you are after:

var list = new List<Tuple<string, string, string>>();
list.Add(new Tuple<string, string, string>("12", "something", "something"));

Although it would probably be better to give these values semantic meaning. Perhaps if you let us know what the values are intending to show, then we can give some ideas on how to make it much more readable.

like image 157
Arran Avatar answered Nov 02 '22 09:11

Arran


Use list of lists:

List<List<string>> read;

Or if you want key-multiple values relation, use dictionary of lists:

Dictionary<string, List<string>> read;
like image 38
Andrei Avatar answered Nov 02 '22 11:11

Andrei