1

I am learning C# and have been taking a lot of online courses. I am looking for a simpler/neater way to enumerate a list within a list.

In python we can do something like this in just one line:

newListofList=[[n,i] for n,i in enumerate([List1,List2,List3])]

Does it have to involve lambda and Linq in C#? if so, what would be the solution? I tried it with Dictionary in C# but my gut tells me this is not a perfect solution.

List<List<string>> familyListss = new List<List<string>>();
familyListss.Add(new List<string> { "Mary", "Mary_sister", "Mary_father", "Mary_mother", "Mary_brother" });
familyListss.Add(new List<string> { "Peter", "Peter_sister", "Peter_father", "Peter_mother", "Peter_brother" });
familyListss.Add(new List<string> { "John", "John_sister", "John_father", "John_mother", "John_brother" });

Dictionary<int, List<string>> familyData = new Dictionary<int, List<string>>();

for (int i = 0; i < familyListss.Count; i++)
{
  familyData.Add(i, familyListss[i]);
}
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Wiley Ng
  • 307
  • 3
  • 14
  • Does this answer your question? [How do you get the index of the current iteration of a foreach loop?](https://stackoverflow.com/questions/43021/how-do-you-get-the-index-of-the-current-iteration-of-a-foreach-loop) – match Dec 31 '21 at 15:49
  • Your python code produces `[[0, [1, 2, 3]], [1, [4, 5, 6]], [2, [7, 8, 9]]]`, which isn't quite a useful thing to work with in C#. What are you trying to do here, and what don't you like about your current solution? – Sweeper Dec 31 '21 at 15:50
  • What do you mean List>, have you considered having an object? – Xserge Dec 31 '21 at 15:50
  • Yes. After reflecting for a while. I think it's silly to have an enumerate list of list... Thank you for all your help. – Wiley Ng Dec 31 '21 at 16:03

2 Answers2

5

Just a constructor will be enough:

List<List<string>> familyListss = new List<List<string>>() {
  new List<string> { "Mary", "Mary_sister", "Mary_father", "Mary_mother", "Mary_brother" },
  new List<string> { "Peter", "Peter_sister", "Peter_father", "Peter_mother", "Peter_brother" },
  new List<string> { "John", "John_sister", "John_father", "John_mother", "John_brother" }
};

If you want to mimic enumerate you can use Linq, Select((value, index) => your lambda here):

using System.Linq;

...

var list = new List<string>() {
  "a", "b", "c", "d"};

var result = list
  .Select((value, index) => $"item[{index}] = {value}");

Console.Write(string.Join(Environment.NewLine, result));

Outcome:

item[0] = a
item[1] = b
item[2] = c
item[3] = d
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
2

Are you taking about something like this?

int i = 0;
familyListss.ForEach(f => { familyData.Add(i, f);i++; });

This is refactored from

int i = 0;
foreach (var f in familyListss)
{
    familyData.Add(i, f);
    i++; 
}

With a small extension method, you can build in an index to foreach to make it one line. Extension methods are worth exploring, and can take annoying, repeated tasks out of your way.

Also see this question: C# Convert List<string> to Dictionary<string, string>

Alex T.
  • 73
  • 8