Home > OS >  How do u check if an element is in a list in which are lists?
How do u check if an element is in a list in which are lists?

Time:01-27

so "alle" is my list in which are more lists and here i wanna check if "user" is in any list of "alle"

so i thought it could be solved by elif user is not in computer and user in alle but yeah i guess it does not work im not into python that much so i realy need your help guys, thank you :)

CodePudding user response:

Instead of a list of lists make a dictionary.

d = {'k_barbaren':k_barbaren, 'k_feuerwerkeren':k_feuerwerkeren, ..., 'k_wut':k_wut,...}

Then iterate over the the items and check:

...
for group,names in d.items():
    if user in names:
        print(group)

CodePudding user response:

for temp_list in alle:
    if "search_string" in temp_list:
        print("the string is in the list")

An easy option would be to iterate over the list. Replace "search_string" with what you are looking for in one of the contained lists. You could also flatten the list, making it one dimensional like this:

flattened = [item for temp_list in alle for item in temp_list]
if "search_string" in flattened:
    print("found")
  • Related