'How to filter a list of strings based on the number of uppercase letters?

How could I display data that has 2 uppercase letters such as "LqdhariT"

data_list = ["98109sdi72", "ais87sdh1", "09821hadi", "hgatruarr", "xkayTakadaPKN", "z9ai8daohad", "q",
             "qkkudsfids3", "tG8012khasd", "ioawuywe", "aqteyegr", "a", "1", "yta", "swqer", "puytar",
             "sx153iar", "vcdsaqurr", "gykhdia", "weqw256465", "equtr", "hhnjklhad", "yatr", "oiu012ih",
             "yataraK", "HaH", "jadK", "iuoiudoa97asdfa", "ioiuodsa", "Kkdaj","iuoiaiua", "oiopjpiayra",
             "1jhkjakjdgar", "nhbvddwqsf", "iuoiaura0", "9801923nna", "7647", "kiraKi", "ouoiaoiad1",
             "zswqKHAIARAja", "yhty", "kueroiuasd", "iariKakdaQ", "x1", "hhyrtG", "xkaryTR4", "asweq",
             "o0192yhasda", "iuoaida", "v", "uoiuoidasa", "i1231har12", "98a09da", "azk", "iouoiadakda",
             "j", "761HUhayK", "kadaduf", "kaanc", "h", "iad", "z", "atadydsa", "iuoai", "iuadada",
             "Lkauda", "urahK", "7yarkK", "zaqwetya", "sdfe", "hasWesW", "iaoida", "ahara", "kwyack",
             "iaidja", "ii3hKIIada", "irajYta2W", "ikdajr", "zawhdyadg", "kerTRE", "iuoiuo2K", "sdeWKrai",
             "kadauyhyk", "kjxkanhg", "kada", "aKajQ", "kadYT", "LqdhariT"]


Solution 1:[1]

You can use a list comprehension with a condition on the case:

out = [item for item in data_list if sum(map(str.isupper, item))==2]

explanation:

For each item in the list, map the str.upper function to each character and count the number of uppercase using sum. If the count is equal to 2, keep the item.

output:

['HaH',
 'iariKakdaQ',
 'xkaryTR4',
 'hasWesW',
 'irajYta2W',
 'sdeWKrai',
 'aKajQ',
 'kadYT',
 'LqdhariT']

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mozway