# How to check if all or half of list members are in certain condition

Example 1

``````if all(enemy.dead for enemy in enemyGroup):
``````

Example 2

``````if sum(enemy.dead for enemy in enemyGroup) >= len(enemyGroup) * 0.5:
``````

These two examples are from Python. `enemyGroup` is a list that contains all enemies. First example is checking if all enemies in enemyGroup list is dead, while the second example is checking if half of enemies in the list are dead.

I can’t figure out if I can or not, write a similar code in C# when manipulating with list. Is there a fancy way to translate this Python code to Unity in C#? If not, what is the most optimal way to check if all or half of members in the list meet certain condition?

Hi, you could create a foreach loop to check all members of your list like:

``````int deadCounter = 0;
foreach (GameObject enemy in enemyGroup)
{
{
}
}

{
}
else if (deadCounter >= enemyGroup.count / 2)
{
//half or more of the enemies are dead
}
``````

You can use Linq for such thing. Add this line at the top

``````using System.Linq;
``````

Now you can do things like that:

``````List<YourClass> list;

// ...

``````

Note you can even convert the items in place. However some linq methods may be quite expensive. The IEnumerable the linq methods create require some memory. You shouldn’t use them every frame.

``````List<GameObject> someGOs;

float sum = someGOs.Select(a=>a.GetComponent<YourScript>()).Where(a=>a.someVariable > 5).Sum(a=>a.fooBar);
``````

This last example is equivalent to:

``````List<GameObject> someGOs;
float sum = 0;
foreach(var go in someGOs)
{
YourScript s = og.GetComponent<YourScript>();
if (s.someVariable > 5)
sum += s.fooBar;
}
``````
{"ft_lang_label":"__label__en","ft_lang_prob":0.7139605,"math_prob":0.72690105,"size":1733,"snap":"2023-40-2023-50","text_gpt3_token_len":442,"char_repetition_ratio":0.12319259,"word_repetition_ratio":0.007194245,"special_character_ratio":0.2596653,"punctuation_ratio":0.15774648,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9801177,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-10-03T21:01:06Z\",\"WARC-Record-ID\":\"<urn:uuid:38c809fe-ce33-4fa1-9ce4-faeed7d88cdd>\",\"Content-Length\":\"24850\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:62fb340f-99bf-4425-80e9-d911b7b3b812>\",\"WARC-Concurrent-To\":\"<urn:uuid:12d4d691-8642-475e-a260-a12c2e23798e>\",\"WARC-IP-Address\":\"184.104.178.75\",\"WARC-Target-URI\":\"https://discussions.unity.com/t/how-to-check-if-all-or-half-of-list-members-are-in-certain-condition/212450\",\"WARC-Payload-Digest\":\"sha1:YJD5TRT7TNAKPKGDQ2LCJVNHJDDZF7SU\",\"WARC-Block-Digest\":\"sha1:7YCOCDB2F7NKMAVSU7QPR3KJVWSWLXOK\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233511220.71_warc_CC-MAIN-20231003192425-20231003222425-00050.warc.gz\"}"}