4

I'm looking for a more efficient way of removing empty string values from a list of a list of strings.

The code below works but for a very large data set this seems inefficient. Is there a more efficient way to do this?

FYI - The beginning is just to build a Data Set to have a list of a list that contains empty strings

public static void Main()
{       
    //Building the data set
    List<List<string>> list = new List<List<string>>();
    list.Add(new List<string> {"One", "Two", "", "Eight"});
    list.Add(new List<string> {"Three", "Five", "Six"});
    list.Add(new List<string> {"Sixteen", "", ""});
    list.Add(new List<string> {"Twenty-Eight", "Forty", "Nine"});

    //Create an empty List of a List
    List<List<string>> newList = new List<List<string>>();

    //Loop through the original list and purge each list of empty strings
    for(int i = 0; i < list.Count; i++) {
        newList.Add(list[i].Where(x => !string.IsNullOrEmpty(x)).ToList());
    }

    foreach (var s in newList) {
        Console.WriteLine(string.Join(", ", s));    
    }

    /*
    CORRECT OUTPUT:
        "One", "Two", "Eight"
        "Three", "Five", "Six"
        "Sixteen"
        "Twenty-Eight", "Forty", "Nine"         
    */      
}
5
  • Since they are lists, you could use List.RemoveAll() instead, that way you're not creating new lists to replace the ones you currently have. Commented Oct 22, 2018 at 19:10
  • Lists are mutable; do you want to mutate the existing lists, or create new ones? Both are reasonable choices. Commented Oct 22, 2018 at 19:16
  • You say that you are worried about "efficiency" but efficiency is value produced divided by resources consumed; what value are you producing, and what resource are you consuming? Commented Oct 22, 2018 at 19:17
  • Good questions. I want to simply remove the empty strings in every child list. Commented Oct 22, 2018 at 19:22
  • 2
    If you want to remove all of a thing, the aptly-named RemoveAll method should be your go-to choice. Commented Oct 22, 2018 at 20:34

4 Answers 4

16

Why not use the List<T>.RemoveAll() method? Definition:

Removes all the elements that match the conditions defined by the specified predicate.

foreach (var l in list)
{
    l.RemoveAll(x => string.IsNullOrEmpty(x));
}

That is all you need. Other answer have Select().Where() and twice ToList(), which is way too much overhead for a simple action like this.

Sign up to request clarification or add additional context in comments.

3 Comments

Interesting.. You believe this is faster than the one-liner LINQ statements above?
@bagofmilk The speed difference is not going to be that much, but this consumes much less memory in that it doesn't need to create a new list of lists.
@bagofmilk: Which one is faster really doesn't matter unless this particular task represents a performance bottleneck in your application. What's more important is that Abbas's code is easier to read. Personally, I prefer the use of method groups (l.RemoveAll(string.IsNullOrEmpty);) instead of lambdas (applies to most of the answers to your question). I'll note that if you insist on a one-liner, you still don't need LINQ: Just use (list.ForEach(x=>x.RemoveAll(String.IsNullOrEmpty))). Though personally, I'm not a fan of List<T>.ForEach.
2

Since you want a List<List<string>> returned then just Select and filter with the inner list with Where

public List<List<string>> FilterStrings()
{
    List<List<string>> list = new List<List<string>>();
    list.Add(new List<string> { "One", "Two", "", "Eight" });
    list.Add(new List<string> { "Three", "Five", "Six" });
    list.Add(new List<string> { "Sixteen", "", "" });
    list.Add(new List<string> { "Twenty-Eight", "Forty", "Nine" });

    return list.Select(lst => lst.Where(str => !string.IsNullOrEmpty(str)).ToList()).ToList();
}

Comments

1
newList = list.Select(t=>t.Where(q=>!string.IsNullOrEmpty(q)).ToList()).ToList();

Comments

0

The main idea: iterate through lists and remove empty strings from each.

list.Select(outerListItem => outerListItem.Where(innerListItem => !string.IsNullOrEmpty(innerListItem).ToList())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.