-1

Background:

my code takes the changes of a git merge request and compares the extentions with a rule file, which says which file was changed (for example implementation file because .py was changed).

Now I want to do a mapping. If something in the implementation files are changed I want to save this information. I just did an boolean implementation_check = False.

My change_log_array list has so many arrays like my rules. If the rule is not triggered the array will stay empty (for example a file with requirements)

Question:

Now I have some empty arrays in my list (in the beginning or in the middle of the list). How can I check if an array is empty? like if x is not None:

  • if arr != [] or something like that –  Oct 22 '20 at 08:43
  • No i tried this out it does not work –  Oct 22 '20 at 08:44
  • It’s as simple as `if not lst:`. If populated, it validates to False, if empty it validates to True. – S3DEV Oct 22 '20 at 08:44
  • 2
    Please dont down vote my question, they gonna ban me :( It took 1 hour for me to ask like that –  Oct 22 '20 at 08:45
  • 4
    u better invest this time learning how to ask bro... for your own good and learning... dont beg for upvotes and downvotes, those are here to help you not to humiliate you – adir abargil Oct 22 '20 at 08:46
  • 1
    What is this "array" type you keep referring to? – user2357112 Oct 22 '20 at 08:48
  • if you get downvotes you get a ban. You can ask 6 months later. I'm a beginner I'm sorry I thought it was clear enough –  Oct 22 '20 at 08:48
  • If you `print(x, type(x), len(x))` before the `if x is not None:` what are the values it can hold ? Can it be None ? What is it's type ? – ygorg Oct 22 '20 at 08:50
  • I did: lst = [] for x in change_log_array: if not lst: print(x) but it still prints: [] [] [{'oldpath....}] –  Oct 22 '20 at 08:54
  • there shouldn't be the empty brackets after the if statement anymore –  Oct 22 '20 at 08:55
  • 1
    @ygorg if its empty: 0 if not empty 64 for ex –  Oct 22 '20 at 08:57
  • @S3DEV I did it like u said, but it still prints empty arrays. How is that possible –  Oct 22 '20 at 09:51
  • @user2357112supportsMonica its a list of arrays with a dictionary in it. So if the array is not emty there is a dict (see output) –  Oct 22 '20 at 10:08
  • That doesn't answer the question at all. "array" is ambiguous - there are a whole bunch of types called "array" in Python, and it's not clear which, if any, you're using. (It doesn't look like a ctypes array, an `array.array` array, or a NumPy array.) – user2357112 Oct 22 '20 at 10:10
  • 1
    Your question depends on all sorts of information you haven't given us, like what the elements of `all_changes` are or what that `to_array` method is. People are assuming your "arrays" are lists, but if they were lists, all the answers they've been giving you would have worked. Your question is completely unanswerable, which is why you've been getting downvotes. Further, clogging Stack Overflow with unanswerable questions is bad for site health, which is why question bans exist. If you don't want to get question-banned, provide enough information to make your question answerable. – user2357112 Oct 22 '20 at 10:16
  • Okay you're right I'm sorry –  Oct 22 '20 at 10:17
  • 1
    As I said Im a beginner, I also work with code from others so I dont understand everything... Im sorry for confusing. –  Oct 22 '20 at 10:19

2 Answers2

-1

Assuming my_list is your variable, then simple:

if my_list:
   # code to execute if my_list exists and is not empty
else:
   # my_list does not exist or is empty

That way it's pythonic :)

ps. checking:

if my_list is not None

simply checks if it exists (has been declared)

-1

What I understood is that you have (sub)lists within a list (myList) and you want to check whether all (sub)lists in the list are still empty (in other words any sub list isn't empty anymore), right? (correct me if I am wrong, so I can edit my answer accordingly).

If so, then maybe with this:

[] == [i for i in myList if i != []]

(Generally, you want to filter out any list that isn't anymore empty, then check that resulted list for emptiness which is empty iff all sublists are empty).

MoeNeuron
  • 78
  • 8
  • could you tell me in my example? I mean I changed my code a bit but it still does not work –  Oct 22 '20 at 09:50
  • Yet, I would recommend rephrasing your question a bit and adding more clarity to what each variable is and the return of some functions. It would be helpful for the readers. – MoeNeuron Oct 22 '20 at 10:02
  • It does not work for me. Thank you though! do you have other tips`? –  Oct 22 '20 at 10:07
  • Please provide us with more insights to your code, if possible, so anyone with no idea with what you are doing, can follow. – MoeNeuron Oct 22 '20 at 10:11
  • did you try using the code above after adding all changes to `change_log_array`? Assign your boolean variable to the code above (with changing `myList` to `change_log_array`) outside the loop – MoeNeuron Oct 22 '20 at 10:13