How I can check if the list in Python is empty

Question:

I have a list of users on the list and want to print for them some greetings but if I have in this list I should print some another text Hello Admin, would you like to see a status report?. Also I want to have some notification if the list is empty but I don’t know how to do this.

users_site = []
for site in users_site:
    if site == 'Admin':
        print ("Hello Admin, would you like to see a status report?")
    if site == []:
        print ("We need more user's")
    else:
        print (f"Hello, {site}")
Asked By: Borys Zhyhalo

||

Answers:

Normally, I’d put everything into a function and short-circuit it early with return – you can detect if a list is empty with not which will see if the right side is Falsey

def my_function(my_list):
    if not my_list:
        print("the list is empty")
        return
    for entry in my_list:
        ...
Answered By: ti7

Does this help?

users_site = ['']

if not users_site:
    print ("We need more user's")
 
else:
    
    for site in users_site:
        if site == 'Admin':
            print ("Hello Admin, would you like to see a status report?")
            
        else:
            print (f"Hello, {site}")
Answered By: band
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.