0

Im using enumerate to obtain the indexes in a list ("i"), but I don't need the value of every element ("j"). "j" is unused in my code, so this is a warning in the eclipse IDE. How I can fix this?

Code:

for i,j in enumerate(my_list):
6
  • 5
    for i in range(len(my_list)):... Commented Feb 14, 2017 at 13:27
  • 1
    This question is unclear. What is it exactly you are trying to do? What exactly is the error that you are seeing? Please provide a working example that reproduces the error. Commented Feb 14, 2017 at 13:27
  • 1
    What @WillemVanOnsem said or for i, _ in enumerate(my_list) if you are talking about a warning by your IDE Commented Feb 14, 2017 at 13:27
  • @schwobaseggl: this is indeed semantically equivalent, but less efficient since here you do not do tuple packing, unpacking, reference counting, etc. Commented Feb 14, 2017 at 13:29
  • 1
    @WillemVanOnsem I am aware. But as a general remark as there are other cases where you do not use a value that is returned by a function, e.g. instance, _ = Foo.objects.get_or_create(...) Commented Feb 14, 2017 at 13:30

2 Answers 2

1

If you want just the index, you can use:

my_index = [i for i in range(len(mylist))]
Sign up to request clarification or add additional context in comments.

Comments

1

Just because an IDE gives you a warning that doesn't mean you have to change your code. @WillemVanOnsem gives you the necessary solution to iterating over the indexes in his comment.

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.