7

So I have this list and variables:

nums = [14, 8, 9, 16, 3, 11, 5]

big = nums[0]

spot = 0

I'm confused about how to actually do it. I want to use this exercise to give me a starter. How do I do that on Python?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
user1998529
  • 51
  • 1
  • 1
  • 4

11 Answers11

15

Usually, you could just use

max(nums)

If you explicitly want to use a loop, try:

max_value = None
for n in nums:
    if max_value is None or n > max_value: max_value = n
helmbert
  • 35,797
  • 13
  • 82
  • 95
  • 1
    What if a number was `-2` in the list? – alex Jan 21 '13 at 23:00
  • 1
    To add some explanation to the loop solution: To get the biggest number, you iterate through the list and remember the biggest number you have encountered so far. That way when you reach the end, you will have the biggest number of them all remembered. – poke Jan 21 '13 at 23:00
  • 1
    @alex You can set `max = nums[0]`; also `max` is not the best variable name for this. – poke Jan 21 '13 at 23:01
  • Granted, the name `max` is ill-chosen. `-1` an initial value fails, if all numbers in the list are < -1. I edited the post accordingly. – helmbert Jan 21 '13 at 23:07
  • 2
    `(int) > None` will raise a TypeError. – poke Jan 22 '13 at 06:30
  • @poke: The expression `-1 > None` evaluates to `True`, at least in Python 2.7. – helmbert Jan 22 '13 at 09:50
  • If `max_value` is initialized to any of the numbers in the list (e.g. `nums[0]`), then we don't need to check for `None` on every loop iteration. It also means the loop only needs to run over the last `len(nums) - 1` entries of the list, instead of `len(nums)` (although I'm not aware of a really elegant way to code that in Python). – Harry Oct 08 '22 at 07:21
10

Here you go...

nums = [14, 8, 9, 16, 3, 11, 5]

big = max(nums)
spot = nums.index(big)

This would be the Pythonic way of achieving this. If you want to use a loop, then loop with the current max value and check if each element is larger, and if so, assign to the current max.

alex
  • 479,566
  • 201
  • 878
  • 984
  • but there is no loop - at least not explicite – tzelleke Jan 21 '13 at 22:58
  • @TheodrosZelleke `for i in range(len(list)): # for loop iterates through it`. That's an explicit way of repeating something for the length of the list. This will work for indices; if you want to do something x amount of times, the `range()` 'end' argument should be x + 1. So to do something x amount of times where x is the length of the list, do: `for i in range(len(list) + 1):`. – Rushy Panchal Jan 21 '13 at 23:21
  • @F3AR3DLEGEND, thanks for the clarification -- however, my comment was meant as a "kind of criticism" not a question. – tzelleke Jan 21 '13 at 23:29
  • `for i in nums: return max(nums)` :p – Ryan Haining Jan 22 '13 at 00:08
  • @F3AR3DLEGEND `list` is a builtin function, best not to use it as a variable name. and you're better off doing `for i, _ in enumerate(seq)` rather than `range(len(seq))` – Ryan Haining Jan 22 '13 at 00:11
  • @xhainingx I was using it to show that it would be a list, not using it as a variable name. I guess `seq` would be more descriptive, though. – Rushy Panchal Jan 22 '13 at 01:45
9
nums = [14, 8, 9, 16, 3, 11, 5]

big = None

spot = None

for i, v in enumerate(nums):
    if big is None or v > big:
         big = v
         spot = i
Emanuele Paolini
  • 9,912
  • 3
  • 38
  • 64
  • 1
    For bonus points, make sure `nums` is not empty before assigning `big = nums[0]`, and if so, raise an exception (`ValueError` would be a good choice). – PaulMcG Jan 21 '13 at 23:12
2

Python already has built in function for this kind of requirement.

list = [3,8,2,9]
max_number = max(list)
print (max_number) # it will print 9 as big number

however if you find the max number with the classic vay you can use loops.

list = [3,8,2,9]

current_max_number = list[0]
for number in list:
    if number>current_max_number:
        current_max_number = number

print (current_max_number) #it will display 9 as big number
Harun ERGUL
  • 5,770
  • 5
  • 53
  • 62
1

Why not simply using the built-in max() function:

>>> m = max(nums)

By the way, some answers to similar questions might be useful:

Community
  • 1
  • 1
Andrea
  • 3,627
  • 4
  • 24
  • 36
1

To address your second question, you can use a for loop:

for i in range(len(list)):
    # do whatever

You should note that range() can have 3 arguments: start, end, and step. Start is what number to start with (if not supplied, it is 0); start is inclusive.. End is where to end at (this has to be give); end is exclusive: if you do range(100), it will give you 0-99. Step is also optional, it means what interval to use. If step is not provided, it will be 1. For example:

>>> x = range(10, 100, 5) # start at 10, end at 101, and use an interval of 5
>>> x
[10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95] # note that it does not hit 100

Since end is exclusive, to include 100, we could do:

>>> x = range(10, 101, 5) # start at 10, end at 101, and use an interval of 5
>>> x
[10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100] # note that it does hit 100
Rushy Panchal
  • 16,979
  • 16
  • 61
  • 94
0

For the Max in List Code HS I've managed to get most of the auto grader to work for me using this code:

list = [-3,-8,-2,0]

current_max_number = list[0]
for number in list:
    if number>current_max_number:
        current_max_number = number

print current_max_number

def max_int_in_list():
    print "Here"

I'm not sure where the max_int_in_list goes though. It needs to have exactly 1 parameter.

dbc
  • 104,963
  • 20
  • 228
  • 340
Jayna
  • 1
  • 2
    *I'm not sure where the max_int_in_list goes though. It needs to have exactly 1 parameter.* -- are you answering this question, or asking a new, related question? Stackoverflow is [not a discussion forum](https://meta.stackexchange.com/a/92110), it's a question and answer site, so if you need a follow-on question answered, you should [delete](https://meta.stackexchange.com/q/25088) this answer, and [ask here](https://stackoverflow.com/questions/ask), possibly linking back to this question for reference. See also: [ask]. – dbc Mar 10 '20 at 16:54
0

To print the Index of the largest number in a list.

numbers = [1,2,3,4,5,6,9]

N = 0
for num in range(len(numbers)) :
  if numbers[num] > N :
    N = numbers[num]
print(numbers.index(N))
0
student_scores[1,2,3,4,5,6,7,8,9]

max=student_scores[0]
for n in range(0,len(student_scores)):
  if student_scores[n]>=max:
    max=student_scores[n]
print(max)
# using for loop to go through all items in the list and assign the biggest value to a variable, which was defined as max.

min=student_scores[0]
for n in range(0,len(student_scores)):
  if student_scores[n]<=min:
    min=student_scores[n]
print(min)
# using for loop to go through all items in the list and assign the smallest value to a variable, which was defined as min.

Note: the above code is to pick up the max and min by using for loop, which can be commonly used in other programming languages as well. However, the max() and min() functions are the easiest way to use in Python to get the same results.

  • Welcome to StackOverflow. While this code may solve the question, [including an explanation](https://meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply. – Ruli Nov 30 '20 at 21:53
0

I would add this as a reference too. You can use the sort and then print the last number.

nums = [14, 8, 9, 16, 3, 11, 5]

nums.sort()
print("Highest number is: ", nums[-1])
-1
scores = [12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23, 24, 25, 26, 27,
          28, 29, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 31, 31, 37,
          56, 75, 23, 565]

# initialize highest to zero
highest = 0

for mark in scores:
    if highest < mark:
        highest = mark

print(mark)
Amro
  • 123,847
  • 25
  • 243
  • 454
ERIC
  • 1