0

So, I need to use a for i in loop on a list that is within a list. Let me give you an example.
I have a blacklist (list) that contains 10 categories (also lists), in those categories, there are always 2 ints.
I also have a list called x. x contains 2 ints.
I want to check if the 2 ints inside x are the same as any of the blacklist categories.
In order to do that I need to do

def blacklist_check(blacklist, z):
    for i in blacklist:
        for y in blacklist[i]:
            difference = blacklist[i][y] - z
            if difference < 10 and difference > -10:
                print(f"{difference} found")
                return False
            print(f"{difference} not found")
            if i == 10:
                return True

but when I try that I get

TypeError: list indices must be integers or slices, not list

I can not transfer the categories to ints or any other type than lists. How do I make this work?

5
  • 2
    Just for y in i and difference = y - z. Note that i is the element in the blacklist, not their index. Commented Jul 4, 2022 at 3:25
  • @MechanicPig Please post this as an answer. Commented Jul 4, 2022 at 3:42
  • @MeLikeFish First debug your program to see what is going on. This article has some great tips to get you started. Then if you still need help post a new quesiton with the current version of your code. Be sure to explicitly explain what "it always returns None" means. Usually with some example output. Commented Jul 4, 2022 at 4:28
  • @Code-Apprentice I found the issue, wanted to edit the comment but it didn't let me. Thank you for the reply though Commented Jul 4, 2022 at 4:37
  • @MeLikeFish Thanks for letting me know. You can only edit a comment within a few minutes of posting it. But you can and should delete comments if they are no longer relevant or helpful. Good luck. Commented Jul 4, 2022 at 7:53

3 Answers 3

1

Here (y) is a list, you cannot nest a list as a index inside another list. Try this

def blacklist_check(blacklist, z):
    for i in blacklist:
        for y in range(len(i)):
            difference = i[y] - z
            if difference < 10 and difference > -10:
                print(f"{difference} found")
                return False
            print(f"{difference} not found")
            if i == 10:
                return True
Sign up to request clarification or add additional context in comments.

3 Comments

I think you mean that i is a list, not (y). Also i[y] should change to y.
Yes, also can use (range and len) function Like the example above
Yes, that works, too. In Python, we prefer for x in my_list as much as possible and don't use range() very much.
0

Python handles for loops differently to perhaps a more conventional C-Type language in that it runs based on enumeration rather than incrementation.

In your case, what you have with for i in blacklist, for each iteration of the for loop, i is set to the element rather than the index, so if your data structure looks something like:

[ [1,2,3,4], [5,6,7,8] ]

Then the first value of i will be [1, 2, 3, 4], and is hence a list. If you want the indexes rather than the elements, you could use the range and len functions in the following way:

for i in range(len(blacklist)):

which will make i be 0 on the first iteration, and 1 on the second one, etc.

That would mean that calling blacklist[i] will return [1, 2, 3, 4]

Comments

0

If you don't consider the printed statements in the sample code, the simplest way to write it should be to use any with generator expression:

def blacklist_check(blacklist, z):
    return not any(abs(y - z) < 10 for cls in blacklist for y in cls)

This is equivalent to:

def blacklist_check(blacklist, z):
    for cls in blacklist:
        for y in cls:
            if abs(y - z) < 10:   # same as -10 < (y - z) < 10
                return False
    return True

Note that since i in your code does not represent the index of the element in the blacklist, you should not judge whether it is the last element by i == 10, but should directly return True after the end of the for loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.