0

As you probably already figured by my question, I am pretty new to programming which is why this question is so basic. But I couldn't really find a question that had an answer to my problem so I thought I might just ask. I have a list that contains integers that I wanted to compare to each other so they can be sorted from highest to lowest. Now for that I wanted to use a for-loop that is executed exactly as many times as there are integers in my list. How do I do that? I tried:

for len(a):    # (a is my list)
    *code*

Thought it is returning me an error, saying that it couldn't be assigned to the function call. I am not sure why :/ . I hope you can help me.

Lenni4000
  • 3
  • 4

1 Answers1

0
for i in range(len(a)):

is the usual way; the official Python tutorial is a good place to start for understanding range(), and the fact that the loop is executed as many times as there are elements in your list when you first entered the loop

kcsquared
  • 5,244
  • 1
  • 11
  • 36
  • Thank you :) what if I have no usage for the variable i tho? Should I just leave it out? – Lenni4000 Sep 23 '21 at 15:29
  • @Lenni4000 Yes, the standard idiom in Python is [to use a single underscore for unused loop variables](https://stackoverflow.com/questions/5477134/how-can-i-get-around-declaring-an-unused-variable-in-a-for-loop) – kcsquared Sep 23 '21 at 15:32
  • Thanks. I appreciate it :) – Lenni4000 Sep 23 '21 at 16:19