-5

Possible Duplicate:
find the maximum number in a list using a loop

I'm finding the maximum number in a list on Python by using a loop. In order to do that, I need to have a loop where it goes through the entire list. What loop can I use that runs through the entire list? I'm new to Python.

Community
  • 1
  • 1
user1998529
  • 51
  • 1
  • 1
  • 4
  • 1
    I would be obliged to help, if I only understood your question. Could you rephrase it a bit? – Hyperboreus Jan 21 '13 at 23:23
  • Check out the python [documentation](http://python.org/doc/). Most likely the answer that you seek is there. – Joel Cornett Jan 21 '13 at 23:25
  • from an earlier comment: [whatcha talkin bout willis?](http://www.youtube.com/watch?v=Qw9oX-kZ_9k) – Rubens Jan 21 '13 at 23:26
  • What was wrong with the (highly upvoted) answers to [the exact same question you asked 30 minutes ago](http://stackoverflow.com/questions/14448692/find-the-maximum-number-in-a-list-using-a-loop)? – David Robinson Jan 21 '13 at 23:29
  • You want to use a `for` loop. Python docs: http://docs.python.org/2/tutorial/controlflow.html – Harpal Jan 21 '13 at 23:31
  • If your struggling with understanding how a for loop works, check out this khan academy video on youtube. http://www.youtube.com/watch?v=9LgyKiq_hU0 – Harpal Jan 21 '13 at 23:35

3 Answers3

1

You can go through a list with a for loop like:

for item in lst:
    # do something with item

However, an easier way to get the maximum item in a list (which appears to be what you want) is:

max(lst)
David Robinson
  • 77,383
  • 16
  • 167
  • 187
0

Repeating a block of code n times, where n is the length of some_list is done like this:

for i in xrange(len(some_list)):
    # block of code

or...

i = 0
while i < len(some_list):
    # block of code
    i = i + 1
SamCode
  • 86
  • 1
  • 12
0
max = None
for e in lst:
    if max is None or e > max: max = e

But as David already stated, simply calling max(lst) would do the job.

Hyperboreus
  • 31,997
  • 9
  • 47
  • 87