-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.

6

3 Answers 3

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)
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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.

2 Comments

Don't call your variable max though
Also note this will break for python 3, where you can't compare NoneType and, say, int.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.