How to find the largest number(s) in a list of elements, possibly non-unique?How do I check if a list is empty?Finding the index of an item given a list containing it in PythonWhat's the simplest way to print a Java array?Convert bytes to a string?Getting the last element of a list in PythonHow to make a flat list out of list of lists?How do I get the number of elements in a list in Python?How do I list all files of a directory?Use of *args and **kwargsHow do I remove a particular element from an array in JavaScript?

Fear of getting stuck on one programming language / technology that is not used in my country

Why "had" in "[something] we would have made had we used [something]"?

Does IPv6 have similar concept of network mask?

Invalid date error by date command

What is going on with 'gets(stdin)' on the site coderbyte?

Mixing PEX brands

Why is this estimator biased?

Can a College of Swords bard use a Blade Flourish option on an opportunity attack provoked by their own Dissonant Whispers spell?

Can a stoichiometric mixture of oxygen and methane exist as a liquid at standard pressure and some (low) temperature?

Redundant comparison & "if" before assignment

Why is it that I can sometimes guess the next note?

Do the primes contain an infinite almost arithmetic progression?

Quoting Keynes in a lecture

Is there an injective, monotonically increasing, strictly concave function from the reals, to the reals?

Store Credit Card Information in Password Manager?

Biological Blimps: Propulsion

What is Cash Advance APR?

Does malloc reserve more space while allocating memory?

Temporarily disable WLAN internet access for children, but allow it for adults

Open a doc from terminal, but not by its name

PTIJ: Haman's bad computer

Creepy dinosaur pc game identification

How can I write humor as character trait?

How do apertures which seem too large to physically fit work?



How to find the largest number(s) in a list of elements, possibly non-unique?


How do I check if a list is empty?Finding the index of an item given a list containing it in PythonWhat's the simplest way to print a Java array?Convert bytes to a string?Getting the last element of a list in PythonHow to make a flat list out of list of lists?How do I get the number of elements in a list in Python?How do I list all files of a directory?Use of *args and **kwargsHow do I remove a particular element from an array in JavaScript?













16















Here is my program,



item_no = []
max = 0
for i in range(5):
input_no = int(input("Enter an item number: "))
item_no.append(input_no)
for i in item_no:
if i > max:
max = i
high = item_no.index(max)
print (item_no[high])


Example input: [5, 6, 7, 8, 8]



Example output: 8



How can I change my program to output the same highest numbers in an array?



Expected output: [8, 8]










share|improve this question









New contributor




user11206537 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 3





    Band indent at line 8

    – DirtyBit
    Mar 18 at 7:21






  • 1





    You can use Dictionary to store the maximum number and its count as item_no = , if you max is different then original in item_no, reinitialize it and add that item and add count =1

    – dkb
    Mar 18 at 7:21







  • 3





    ("Band" probably is a misspelling for "Bad")

    – tripleee
    Mar 18 at 7:26






  • 1





    @tripleee Indeed. bulls-eye!

    – DirtyBit
    Mar 18 at 7:39






  • 1





    As answers show, it's more Pythonic to do it with list comprehensions, rather than loops. But if you use loops to iterate over the list, a style tip: don't use the same variable name i both for indices i in range(5) then also for items/values: for i in item_no. Better to do for no in item_no

    – smci
    Mar 19 at 0:35
















16















Here is my program,



item_no = []
max = 0
for i in range(5):
input_no = int(input("Enter an item number: "))
item_no.append(input_no)
for i in item_no:
if i > max:
max = i
high = item_no.index(max)
print (item_no[high])


Example input: [5, 6, 7, 8, 8]



Example output: 8



How can I change my program to output the same highest numbers in an array?



Expected output: [8, 8]










share|improve this question









New contributor




user11206537 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 3





    Band indent at line 8

    – DirtyBit
    Mar 18 at 7:21






  • 1





    You can use Dictionary to store the maximum number and its count as item_no = , if you max is different then original in item_no, reinitialize it and add that item and add count =1

    – dkb
    Mar 18 at 7:21







  • 3





    ("Band" probably is a misspelling for "Bad")

    – tripleee
    Mar 18 at 7:26






  • 1





    @tripleee Indeed. bulls-eye!

    – DirtyBit
    Mar 18 at 7:39






  • 1





    As answers show, it's more Pythonic to do it with list comprehensions, rather than loops. But if you use loops to iterate over the list, a style tip: don't use the same variable name i both for indices i in range(5) then also for items/values: for i in item_no. Better to do for no in item_no

    – smci
    Mar 19 at 0:35














16












16








16


4






Here is my program,



item_no = []
max = 0
for i in range(5):
input_no = int(input("Enter an item number: "))
item_no.append(input_no)
for i in item_no:
if i > max:
max = i
high = item_no.index(max)
print (item_no[high])


Example input: [5, 6, 7, 8, 8]



Example output: 8



How can I change my program to output the same highest numbers in an array?



Expected output: [8, 8]










share|improve this question









New contributor




user11206537 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












Here is my program,



item_no = []
max = 0
for i in range(5):
input_no = int(input("Enter an item number: "))
item_no.append(input_no)
for i in item_no:
if i > max:
max = i
high = item_no.index(max)
print (item_no[high])


Example input: [5, 6, 7, 8, 8]



Example output: 8



How can I change my program to output the same highest numbers in an array?



Expected output: [8, 8]







python arrays python-3.x






share|improve this question









New contributor




user11206537 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




user11206537 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Mar 19 at 0:38









smci

15.4k678109




15.4k678109






New contributor




user11206537 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Mar 18 at 7:19









user11206537user11206537

91112




91112




New contributor




user11206537 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





user11206537 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






user11206537 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 3





    Band indent at line 8

    – DirtyBit
    Mar 18 at 7:21






  • 1





    You can use Dictionary to store the maximum number and its count as item_no = , if you max is different then original in item_no, reinitialize it and add that item and add count =1

    – dkb
    Mar 18 at 7:21







  • 3





    ("Band" probably is a misspelling for "Bad")

    – tripleee
    Mar 18 at 7:26






  • 1





    @tripleee Indeed. bulls-eye!

    – DirtyBit
    Mar 18 at 7:39






  • 1





    As answers show, it's more Pythonic to do it with list comprehensions, rather than loops. But if you use loops to iterate over the list, a style tip: don't use the same variable name i both for indices i in range(5) then also for items/values: for i in item_no. Better to do for no in item_no

    – smci
    Mar 19 at 0:35













  • 3





    Band indent at line 8

    – DirtyBit
    Mar 18 at 7:21






  • 1





    You can use Dictionary to store the maximum number and its count as item_no = , if you max is different then original in item_no, reinitialize it and add that item and add count =1

    – dkb
    Mar 18 at 7:21







  • 3





    ("Band" probably is a misspelling for "Bad")

    – tripleee
    Mar 18 at 7:26






  • 1





    @tripleee Indeed. bulls-eye!

    – DirtyBit
    Mar 18 at 7:39






  • 1





    As answers show, it's more Pythonic to do it with list comprehensions, rather than loops. But if you use loops to iterate over the list, a style tip: don't use the same variable name i both for indices i in range(5) then also for items/values: for i in item_no. Better to do for no in item_no

    – smci
    Mar 19 at 0:35








3




3





Band indent at line 8

– DirtyBit
Mar 18 at 7:21





Band indent at line 8

– DirtyBit
Mar 18 at 7:21




1




1





You can use Dictionary to store the maximum number and its count as item_no = , if you max is different then original in item_no, reinitialize it and add that item and add count =1

– dkb
Mar 18 at 7:21






You can use Dictionary to store the maximum number and its count as item_no = , if you max is different then original in item_no, reinitialize it and add that item and add count =1

– dkb
Mar 18 at 7:21





3




3





("Band" probably is a misspelling for "Bad")

– tripleee
Mar 18 at 7:26





("Band" probably is a misspelling for "Bad")

– tripleee
Mar 18 at 7:26




1




1





@tripleee Indeed. bulls-eye!

– DirtyBit
Mar 18 at 7:39





@tripleee Indeed. bulls-eye!

– DirtyBit
Mar 18 at 7:39




1




1





As answers show, it's more Pythonic to do it with list comprehensions, rather than loops. But if you use loops to iterate over the list, a style tip: don't use the same variable name i both for indices i in range(5) then also for items/values: for i in item_no. Better to do for no in item_no

– smci
Mar 19 at 0:35






As answers show, it's more Pythonic to do it with list comprehensions, rather than loops. But if you use loops to iterate over the list, a style tip: don't use the same variable name i both for indices i in range(5) then also for items/values: for i in item_no. Better to do for no in item_no

– smci
Mar 19 at 0:35













6 Answers
6






active

oldest

votes


















27














Just get the maximum using max and then its count and combine the two in a list-comprehension.



item_no = [5, 6, 7, 8, 8]

max_no = max(item_no)
highest = [max_no for _ in range(item_no.count(max_no))]
print(highest) # -> [8, 8]


Note that this will return a list of a single item in case your maximum value appears only once.




A solution closer to your current programming style would be the following:



item_no = [5, 6, 7, 8, 8]
max_no = 0 # Note 1
for i in item_no:
if i > max_no:
max_no = i
high = [i]
elif i == max_no:
high.append(i)


with the same results as above of course.



Notes



  1. I am assuming that you are dealing with N* (1, 2, ...) numbers only. If that is not the case, initializing with -math.inf should be used instead.


Note that the second code snippet is less efficient than the first by quite a margin. Python allows you to be more efficient than these explicit, fortran-like loops and it is more efficient itself when you use it properly.






share|improve this answer

























  • Actually, I would love to use the first code, but since this part of my school project, I am not allowed to use inbuilt functions, therefore I need to use the second code.

    – user11206537
    Mar 18 at 7:42






  • 2





    @user11206537 I kinda saw this coming. Knowing what a better way would be is still of some value. Have fun!

    – Ev. Kounis
    Mar 18 at 7:44











  • One last question, how do you find the index of the result (high) in item_no?

    – user11206537
    Mar 18 at 8:32







  • 3





    @user11206537 To get the index as well, you can modify the code slightly and use enumerate on item_no. As you loop through, store the index in a 2nd list as you do for the max. Take a look at this

    – Ev. Kounis
    Mar 18 at 9:32






  • 1





    @Drew Yes, you're right; if item_no[0] == 0, the code crashes as high isn't defined when reaching high.append(i).

    – PiCTo
    Mar 19 at 0:03


















15














You can do it even shorter:



item_no = [5, 6, 7, 8, 8]
#compute once - use many
max_item = max(item_no)
print(item_no.count(max_item) * [max_item])


output:



[8, 8]





share|improve this answer

























  • One last question, how do you find the index of the result in item_no?

    – user11206537
    Mar 18 at 8:48



















4














You could use list comprehension for that task following way:



numbers = [5, 6, 7, 8, 8]
maxnumbers = [i for i in numbers if i==max(numbers)]
print(*maxnumbers,sep=',')


output:



8,8


* operator in print is used to unpack values, sep is used to inform print what seperator to use: , in this case.



EDIT: If you want to get indices of biggest value and call max only once then do:



numbers = [5, 6, 7, 8, 8]
biggest = max(numbers)
positions = [inx for inx,i in enumerate(numbers) if i==biggest]
print(*positions,sep=',')


Output:



3,4


As you might check numbers[3] is equal to biggest and numbers[4] is equal to biggest.






share|improve this answer




















  • 4





    note that your solution calls max a total of len(numbers) times. Storing it outside the list and then using that would be better.

    – Ev. Kounis
    Mar 18 at 7:31












  • How do you find the index of the result in item_no?

    – user11206537
    Mar 18 at 9:01











  • @Ev.Kounis I assume the Python interpreter optimizes this, no?

    – user1717828
    Mar 18 at 16:38











  • @user1717828 No it does not.

    – Ev. Kounis
    Mar 19 at 7:04











  • @Ev.Kounis, Oh man, I didn't believe this so I tested it out. python -m timeit "numbers = [5, 6, 7, 8, 8]; max_number = max(numbers); maxnumbers = [i for i in numbers if i==max_number]" is twice as fast as when the max calculation is in the comprehension. I've gotten very spoiled from using languages with compilers.

    – user1717828
    Mar 19 at 9:53



















3














  1. Count the occurrence of max number


  2. iterate over the list to print the max number for the range of the count (1)


Hence:



item_no = [5, 6, 7, 8, 8]
counter = item_no.count(max(item_no)) # 2
print([max(item_no) for x in range(counter)])


OUTPUT:



[8, 8]





share|improve this answer























  • How do you find the index of the result in item_no?

    – user11206537
    Mar 18 at 8:56


















2














This issue can be solved in one line, by finding an item which is equal to the maximum value:
To improve performance store max in var
Mvalue=max(item_no)
[i for i in item_no if i==Mvalue]






share|improve this answer




















  • 1





    How do you find the index of the result in item_no?

    – user11206537
    Mar 18 at 9:01











  • By using enumerate function, we can get index position: [i for i, val in enumerate(item_no) if val in result]

    – Pradeep Pandey
    Mar 18 at 11:56












  • But isn't max called n times? Not efficient.

    – Sachin Dangol
    Mar 20 at 1:10


















1














I think it would be better if we evaluate the max in the array and its count in one iteration



def maxs(iterable):
max = None
count = 0
for index, value in enumerate(iterable):
if index == 0 or value >= max:
if value != max:
count = 0
max = value
count += 1
return count * [max]


print (maxs([5, 6, 7, 8, 8])) # [8, 8]
print (maxs([3, 2, 4, 5, 1, 2, 4, 5, 2, 5, 0])) # [5, 5, 5]
print (maxs([])) # []


Give it a Try!!






share|improve this answer























  • I did! And it worked! That's a very efficient solution.

    – user11206537
    2 days ago











  • @user11206537 Did you measure and compared the efficiency among the solutions. Can you share with us?

    – Grijesh Chauhan
    2 days ago











  • This kind of solution was the one that was actually needed as a part of my school project and my teacher thought of it as a very efficient solution. This code is also written along the lines of my current programming style, which is exactly what I wanted.

    – user11206537
    2 days ago










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);






user11206537 is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55216278%2fhow-to-find-the-largest-numbers-in-a-list-of-elements-possibly-non-unique%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























6 Answers
6






active

oldest

votes








6 Answers
6






active

oldest

votes









active

oldest

votes






active

oldest

votes









27














Just get the maximum using max and then its count and combine the two in a list-comprehension.



item_no = [5, 6, 7, 8, 8]

max_no = max(item_no)
highest = [max_no for _ in range(item_no.count(max_no))]
print(highest) # -> [8, 8]


Note that this will return a list of a single item in case your maximum value appears only once.




A solution closer to your current programming style would be the following:



item_no = [5, 6, 7, 8, 8]
max_no = 0 # Note 1
for i in item_no:
if i > max_no:
max_no = i
high = [i]
elif i == max_no:
high.append(i)


with the same results as above of course.



Notes



  1. I am assuming that you are dealing with N* (1, 2, ...) numbers only. If that is not the case, initializing with -math.inf should be used instead.


Note that the second code snippet is less efficient than the first by quite a margin. Python allows you to be more efficient than these explicit, fortran-like loops and it is more efficient itself when you use it properly.






share|improve this answer

























  • Actually, I would love to use the first code, but since this part of my school project, I am not allowed to use inbuilt functions, therefore I need to use the second code.

    – user11206537
    Mar 18 at 7:42






  • 2





    @user11206537 I kinda saw this coming. Knowing what a better way would be is still of some value. Have fun!

    – Ev. Kounis
    Mar 18 at 7:44











  • One last question, how do you find the index of the result (high) in item_no?

    – user11206537
    Mar 18 at 8:32







  • 3





    @user11206537 To get the index as well, you can modify the code slightly and use enumerate on item_no. As you loop through, store the index in a 2nd list as you do for the max. Take a look at this

    – Ev. Kounis
    Mar 18 at 9:32






  • 1





    @Drew Yes, you're right; if item_no[0] == 0, the code crashes as high isn't defined when reaching high.append(i).

    – PiCTo
    Mar 19 at 0:03















27














Just get the maximum using max and then its count and combine the two in a list-comprehension.



item_no = [5, 6, 7, 8, 8]

max_no = max(item_no)
highest = [max_no for _ in range(item_no.count(max_no))]
print(highest) # -> [8, 8]


Note that this will return a list of a single item in case your maximum value appears only once.




A solution closer to your current programming style would be the following:



item_no = [5, 6, 7, 8, 8]
max_no = 0 # Note 1
for i in item_no:
if i > max_no:
max_no = i
high = [i]
elif i == max_no:
high.append(i)


with the same results as above of course.



Notes



  1. I am assuming that you are dealing with N* (1, 2, ...) numbers only. If that is not the case, initializing with -math.inf should be used instead.


Note that the second code snippet is less efficient than the first by quite a margin. Python allows you to be more efficient than these explicit, fortran-like loops and it is more efficient itself when you use it properly.






share|improve this answer

























  • Actually, I would love to use the first code, but since this part of my school project, I am not allowed to use inbuilt functions, therefore I need to use the second code.

    – user11206537
    Mar 18 at 7:42






  • 2





    @user11206537 I kinda saw this coming. Knowing what a better way would be is still of some value. Have fun!

    – Ev. Kounis
    Mar 18 at 7:44











  • One last question, how do you find the index of the result (high) in item_no?

    – user11206537
    Mar 18 at 8:32







  • 3





    @user11206537 To get the index as well, you can modify the code slightly and use enumerate on item_no. As you loop through, store the index in a 2nd list as you do for the max. Take a look at this

    – Ev. Kounis
    Mar 18 at 9:32






  • 1





    @Drew Yes, you're right; if item_no[0] == 0, the code crashes as high isn't defined when reaching high.append(i).

    – PiCTo
    Mar 19 at 0:03













27












27








27







Just get the maximum using max and then its count and combine the two in a list-comprehension.



item_no = [5, 6, 7, 8, 8]

max_no = max(item_no)
highest = [max_no for _ in range(item_no.count(max_no))]
print(highest) # -> [8, 8]


Note that this will return a list of a single item in case your maximum value appears only once.




A solution closer to your current programming style would be the following:



item_no = [5, 6, 7, 8, 8]
max_no = 0 # Note 1
for i in item_no:
if i > max_no:
max_no = i
high = [i]
elif i == max_no:
high.append(i)


with the same results as above of course.



Notes



  1. I am assuming that you are dealing with N* (1, 2, ...) numbers only. If that is not the case, initializing with -math.inf should be used instead.


Note that the second code snippet is less efficient than the first by quite a margin. Python allows you to be more efficient than these explicit, fortran-like loops and it is more efficient itself when you use it properly.






share|improve this answer















Just get the maximum using max and then its count and combine the two in a list-comprehension.



item_no = [5, 6, 7, 8, 8]

max_no = max(item_no)
highest = [max_no for _ in range(item_no.count(max_no))]
print(highest) # -> [8, 8]


Note that this will return a list of a single item in case your maximum value appears only once.




A solution closer to your current programming style would be the following:



item_no = [5, 6, 7, 8, 8]
max_no = 0 # Note 1
for i in item_no:
if i > max_no:
max_no = i
high = [i]
elif i == max_no:
high.append(i)


with the same results as above of course.



Notes



  1. I am assuming that you are dealing with N* (1, 2, ...) numbers only. If that is not the case, initializing with -math.inf should be used instead.


Note that the second code snippet is less efficient than the first by quite a margin. Python allows you to be more efficient than these explicit, fortran-like loops and it is more efficient itself when you use it properly.







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 19 at 7:10

























answered Mar 18 at 7:22









Ev. KounisEv. Kounis

11.3k21751




11.3k21751












  • Actually, I would love to use the first code, but since this part of my school project, I am not allowed to use inbuilt functions, therefore I need to use the second code.

    – user11206537
    Mar 18 at 7:42






  • 2





    @user11206537 I kinda saw this coming. Knowing what a better way would be is still of some value. Have fun!

    – Ev. Kounis
    Mar 18 at 7:44











  • One last question, how do you find the index of the result (high) in item_no?

    – user11206537
    Mar 18 at 8:32







  • 3





    @user11206537 To get the index as well, you can modify the code slightly and use enumerate on item_no. As you loop through, store the index in a 2nd list as you do for the max. Take a look at this

    – Ev. Kounis
    Mar 18 at 9:32






  • 1





    @Drew Yes, you're right; if item_no[0] == 0, the code crashes as high isn't defined when reaching high.append(i).

    – PiCTo
    Mar 19 at 0:03

















  • Actually, I would love to use the first code, but since this part of my school project, I am not allowed to use inbuilt functions, therefore I need to use the second code.

    – user11206537
    Mar 18 at 7:42






  • 2





    @user11206537 I kinda saw this coming. Knowing what a better way would be is still of some value. Have fun!

    – Ev. Kounis
    Mar 18 at 7:44











  • One last question, how do you find the index of the result (high) in item_no?

    – user11206537
    Mar 18 at 8:32







  • 3





    @user11206537 To get the index as well, you can modify the code slightly and use enumerate on item_no. As you loop through, store the index in a 2nd list as you do for the max. Take a look at this

    – Ev. Kounis
    Mar 18 at 9:32






  • 1





    @Drew Yes, you're right; if item_no[0] == 0, the code crashes as high isn't defined when reaching high.append(i).

    – PiCTo
    Mar 19 at 0:03
















Actually, I would love to use the first code, but since this part of my school project, I am not allowed to use inbuilt functions, therefore I need to use the second code.

– user11206537
Mar 18 at 7:42





Actually, I would love to use the first code, but since this part of my school project, I am not allowed to use inbuilt functions, therefore I need to use the second code.

– user11206537
Mar 18 at 7:42




2




2





@user11206537 I kinda saw this coming. Knowing what a better way would be is still of some value. Have fun!

– Ev. Kounis
Mar 18 at 7:44





@user11206537 I kinda saw this coming. Knowing what a better way would be is still of some value. Have fun!

– Ev. Kounis
Mar 18 at 7:44













One last question, how do you find the index of the result (high) in item_no?

– user11206537
Mar 18 at 8:32






One last question, how do you find the index of the result (high) in item_no?

– user11206537
Mar 18 at 8:32





3




3





@user11206537 To get the index as well, you can modify the code slightly and use enumerate on item_no. As you loop through, store the index in a 2nd list as you do for the max. Take a look at this

– Ev. Kounis
Mar 18 at 9:32





@user11206537 To get the index as well, you can modify the code slightly and use enumerate on item_no. As you loop through, store the index in a 2nd list as you do for the max. Take a look at this

– Ev. Kounis
Mar 18 at 9:32




1




1





@Drew Yes, you're right; if item_no[0] == 0, the code crashes as high isn't defined when reaching high.append(i).

– PiCTo
Mar 19 at 0:03





@Drew Yes, you're right; if item_no[0] == 0, the code crashes as high isn't defined when reaching high.append(i).

– PiCTo
Mar 19 at 0:03













15














You can do it even shorter:



item_no = [5, 6, 7, 8, 8]
#compute once - use many
max_item = max(item_no)
print(item_no.count(max_item) * [max_item])


output:



[8, 8]





share|improve this answer

























  • One last question, how do you find the index of the result in item_no?

    – user11206537
    Mar 18 at 8:48
















15














You can do it even shorter:



item_no = [5, 6, 7, 8, 8]
#compute once - use many
max_item = max(item_no)
print(item_no.count(max_item) * [max_item])


output:



[8, 8]





share|improve this answer

























  • One last question, how do you find the index of the result in item_no?

    – user11206537
    Mar 18 at 8:48














15












15








15







You can do it even shorter:



item_no = [5, 6, 7, 8, 8]
#compute once - use many
max_item = max(item_no)
print(item_no.count(max_item) * [max_item])


output:



[8, 8]





share|improve this answer















You can do it even shorter:



item_no = [5, 6, 7, 8, 8]
#compute once - use many
max_item = max(item_no)
print(item_no.count(max_item) * [max_item])


output:



[8, 8]






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 18 at 11:40

























answered Mar 18 at 7:30









AllanAllan

8,05231535




8,05231535












  • One last question, how do you find the index of the result in item_no?

    – user11206537
    Mar 18 at 8:48


















  • One last question, how do you find the index of the result in item_no?

    – user11206537
    Mar 18 at 8:48

















One last question, how do you find the index of the result in item_no?

– user11206537
Mar 18 at 8:48






One last question, how do you find the index of the result in item_no?

– user11206537
Mar 18 at 8:48












4














You could use list comprehension for that task following way:



numbers = [5, 6, 7, 8, 8]
maxnumbers = [i for i in numbers if i==max(numbers)]
print(*maxnumbers,sep=',')


output:



8,8


* operator in print is used to unpack values, sep is used to inform print what seperator to use: , in this case.



EDIT: If you want to get indices of biggest value and call max only once then do:



numbers = [5, 6, 7, 8, 8]
biggest = max(numbers)
positions = [inx for inx,i in enumerate(numbers) if i==biggest]
print(*positions,sep=',')


Output:



3,4


As you might check numbers[3] is equal to biggest and numbers[4] is equal to biggest.






share|improve this answer




















  • 4





    note that your solution calls max a total of len(numbers) times. Storing it outside the list and then using that would be better.

    – Ev. Kounis
    Mar 18 at 7:31












  • How do you find the index of the result in item_no?

    – user11206537
    Mar 18 at 9:01











  • @Ev.Kounis I assume the Python interpreter optimizes this, no?

    – user1717828
    Mar 18 at 16:38











  • @user1717828 No it does not.

    – Ev. Kounis
    Mar 19 at 7:04











  • @Ev.Kounis, Oh man, I didn't believe this so I tested it out. python -m timeit "numbers = [5, 6, 7, 8, 8]; max_number = max(numbers); maxnumbers = [i for i in numbers if i==max_number]" is twice as fast as when the max calculation is in the comprehension. I've gotten very spoiled from using languages with compilers.

    – user1717828
    Mar 19 at 9:53
















4














You could use list comprehension for that task following way:



numbers = [5, 6, 7, 8, 8]
maxnumbers = [i for i in numbers if i==max(numbers)]
print(*maxnumbers,sep=',')


output:



8,8


* operator in print is used to unpack values, sep is used to inform print what seperator to use: , in this case.



EDIT: If you want to get indices of biggest value and call max only once then do:



numbers = [5, 6, 7, 8, 8]
biggest = max(numbers)
positions = [inx for inx,i in enumerate(numbers) if i==biggest]
print(*positions,sep=',')


Output:



3,4


As you might check numbers[3] is equal to biggest and numbers[4] is equal to biggest.






share|improve this answer




















  • 4





    note that your solution calls max a total of len(numbers) times. Storing it outside the list and then using that would be better.

    – Ev. Kounis
    Mar 18 at 7:31












  • How do you find the index of the result in item_no?

    – user11206537
    Mar 18 at 9:01











  • @Ev.Kounis I assume the Python interpreter optimizes this, no?

    – user1717828
    Mar 18 at 16:38











  • @user1717828 No it does not.

    – Ev. Kounis
    Mar 19 at 7:04











  • @Ev.Kounis, Oh man, I didn't believe this so I tested it out. python -m timeit "numbers = [5, 6, 7, 8, 8]; max_number = max(numbers); maxnumbers = [i for i in numbers if i==max_number]" is twice as fast as when the max calculation is in the comprehension. I've gotten very spoiled from using languages with compilers.

    – user1717828
    Mar 19 at 9:53














4












4








4







You could use list comprehension for that task following way:



numbers = [5, 6, 7, 8, 8]
maxnumbers = [i for i in numbers if i==max(numbers)]
print(*maxnumbers,sep=',')


output:



8,8


* operator in print is used to unpack values, sep is used to inform print what seperator to use: , in this case.



EDIT: If you want to get indices of biggest value and call max only once then do:



numbers = [5, 6, 7, 8, 8]
biggest = max(numbers)
positions = [inx for inx,i in enumerate(numbers) if i==biggest]
print(*positions,sep=',')


Output:



3,4


As you might check numbers[3] is equal to biggest and numbers[4] is equal to biggest.






share|improve this answer















You could use list comprehension for that task following way:



numbers = [5, 6, 7, 8, 8]
maxnumbers = [i for i in numbers if i==max(numbers)]
print(*maxnumbers,sep=',')


output:



8,8


* operator in print is used to unpack values, sep is used to inform print what seperator to use: , in this case.



EDIT: If you want to get indices of biggest value and call max only once then do:



numbers = [5, 6, 7, 8, 8]
biggest = max(numbers)
positions = [inx for inx,i in enumerate(numbers) if i==biggest]
print(*positions,sep=',')


Output:



3,4


As you might check numbers[3] is equal to biggest and numbers[4] is equal to biggest.







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 18 at 10:02

























answered Mar 18 at 7:29









DaweoDaweo

1,08525




1,08525







  • 4





    note that your solution calls max a total of len(numbers) times. Storing it outside the list and then using that would be better.

    – Ev. Kounis
    Mar 18 at 7:31












  • How do you find the index of the result in item_no?

    – user11206537
    Mar 18 at 9:01











  • @Ev.Kounis I assume the Python interpreter optimizes this, no?

    – user1717828
    Mar 18 at 16:38











  • @user1717828 No it does not.

    – Ev. Kounis
    Mar 19 at 7:04











  • @Ev.Kounis, Oh man, I didn't believe this so I tested it out. python -m timeit "numbers = [5, 6, 7, 8, 8]; max_number = max(numbers); maxnumbers = [i for i in numbers if i==max_number]" is twice as fast as when the max calculation is in the comprehension. I've gotten very spoiled from using languages with compilers.

    – user1717828
    Mar 19 at 9:53













  • 4





    note that your solution calls max a total of len(numbers) times. Storing it outside the list and then using that would be better.

    – Ev. Kounis
    Mar 18 at 7:31












  • How do you find the index of the result in item_no?

    – user11206537
    Mar 18 at 9:01











  • @Ev.Kounis I assume the Python interpreter optimizes this, no?

    – user1717828
    Mar 18 at 16:38











  • @user1717828 No it does not.

    – Ev. Kounis
    Mar 19 at 7:04











  • @Ev.Kounis, Oh man, I didn't believe this so I tested it out. python -m timeit "numbers = [5, 6, 7, 8, 8]; max_number = max(numbers); maxnumbers = [i for i in numbers if i==max_number]" is twice as fast as when the max calculation is in the comprehension. I've gotten very spoiled from using languages with compilers.

    – user1717828
    Mar 19 at 9:53








4




4





note that your solution calls max a total of len(numbers) times. Storing it outside the list and then using that would be better.

– Ev. Kounis
Mar 18 at 7:31






note that your solution calls max a total of len(numbers) times. Storing it outside the list and then using that would be better.

– Ev. Kounis
Mar 18 at 7:31














How do you find the index of the result in item_no?

– user11206537
Mar 18 at 9:01





How do you find the index of the result in item_no?

– user11206537
Mar 18 at 9:01













@Ev.Kounis I assume the Python interpreter optimizes this, no?

– user1717828
Mar 18 at 16:38





@Ev.Kounis I assume the Python interpreter optimizes this, no?

– user1717828
Mar 18 at 16:38













@user1717828 No it does not.

– Ev. Kounis
Mar 19 at 7:04





@user1717828 No it does not.

– Ev. Kounis
Mar 19 at 7:04













@Ev.Kounis, Oh man, I didn't believe this so I tested it out. python -m timeit "numbers = [5, 6, 7, 8, 8]; max_number = max(numbers); maxnumbers = [i for i in numbers if i==max_number]" is twice as fast as when the max calculation is in the comprehension. I've gotten very spoiled from using languages with compilers.

– user1717828
Mar 19 at 9:53






@Ev.Kounis, Oh man, I didn't believe this so I tested it out. python -m timeit "numbers = [5, 6, 7, 8, 8]; max_number = max(numbers); maxnumbers = [i for i in numbers if i==max_number]" is twice as fast as when the max calculation is in the comprehension. I've gotten very spoiled from using languages with compilers.

– user1717828
Mar 19 at 9:53












3














  1. Count the occurrence of max number


  2. iterate over the list to print the max number for the range of the count (1)


Hence:



item_no = [5, 6, 7, 8, 8]
counter = item_no.count(max(item_no)) # 2
print([max(item_no) for x in range(counter)])


OUTPUT:



[8, 8]





share|improve this answer























  • How do you find the index of the result in item_no?

    – user11206537
    Mar 18 at 8:56















3














  1. Count the occurrence of max number


  2. iterate over the list to print the max number for the range of the count (1)


Hence:



item_no = [5, 6, 7, 8, 8]
counter = item_no.count(max(item_no)) # 2
print([max(item_no) for x in range(counter)])


OUTPUT:



[8, 8]





share|improve this answer























  • How do you find the index of the result in item_no?

    – user11206537
    Mar 18 at 8:56













3












3








3







  1. Count the occurrence of max number


  2. iterate over the list to print the max number for the range of the count (1)


Hence:



item_no = [5, 6, 7, 8, 8]
counter = item_no.count(max(item_no)) # 2
print([max(item_no) for x in range(counter)])


OUTPUT:



[8, 8]





share|improve this answer













  1. Count the occurrence of max number


  2. iterate over the list to print the max number for the range of the count (1)


Hence:



item_no = [5, 6, 7, 8, 8]
counter = item_no.count(max(item_no)) # 2
print([max(item_no) for x in range(counter)])


OUTPUT:



[8, 8]






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 18 at 7:35









DirtyBitDirtyBit

10.1k21540




10.1k21540












  • How do you find the index of the result in item_no?

    – user11206537
    Mar 18 at 8:56

















  • How do you find the index of the result in item_no?

    – user11206537
    Mar 18 at 8:56
















How do you find the index of the result in item_no?

– user11206537
Mar 18 at 8:56





How do you find the index of the result in item_no?

– user11206537
Mar 18 at 8:56











2














This issue can be solved in one line, by finding an item which is equal to the maximum value:
To improve performance store max in var
Mvalue=max(item_no)
[i for i in item_no if i==Mvalue]






share|improve this answer




















  • 1





    How do you find the index of the result in item_no?

    – user11206537
    Mar 18 at 9:01











  • By using enumerate function, we can get index position: [i for i, val in enumerate(item_no) if val in result]

    – Pradeep Pandey
    Mar 18 at 11:56












  • But isn't max called n times? Not efficient.

    – Sachin Dangol
    Mar 20 at 1:10















2














This issue can be solved in one line, by finding an item which is equal to the maximum value:
To improve performance store max in var
Mvalue=max(item_no)
[i for i in item_no if i==Mvalue]






share|improve this answer




















  • 1





    How do you find the index of the result in item_no?

    – user11206537
    Mar 18 at 9:01











  • By using enumerate function, we can get index position: [i for i, val in enumerate(item_no) if val in result]

    – Pradeep Pandey
    Mar 18 at 11:56












  • But isn't max called n times? Not efficient.

    – Sachin Dangol
    Mar 20 at 1:10













2












2








2







This issue can be solved in one line, by finding an item which is equal to the maximum value:
To improve performance store max in var
Mvalue=max(item_no)
[i for i in item_no if i==Mvalue]






share|improve this answer















This issue can be solved in one line, by finding an item which is equal to the maximum value:
To improve performance store max in var
Mvalue=max(item_no)
[i for i in item_no if i==Mvalue]







share|improve this answer














share|improve this answer



share|improve this answer








edited yesterday

























answered Mar 18 at 8:06









Pradeep PandeyPradeep Pandey

17917




17917







  • 1





    How do you find the index of the result in item_no?

    – user11206537
    Mar 18 at 9:01











  • By using enumerate function, we can get index position: [i for i, val in enumerate(item_no) if val in result]

    – Pradeep Pandey
    Mar 18 at 11:56












  • But isn't max called n times? Not efficient.

    – Sachin Dangol
    Mar 20 at 1:10












  • 1





    How do you find the index of the result in item_no?

    – user11206537
    Mar 18 at 9:01











  • By using enumerate function, we can get index position: [i for i, val in enumerate(item_no) if val in result]

    – Pradeep Pandey
    Mar 18 at 11:56












  • But isn't max called n times? Not efficient.

    – Sachin Dangol
    Mar 20 at 1:10







1




1





How do you find the index of the result in item_no?

– user11206537
Mar 18 at 9:01





How do you find the index of the result in item_no?

– user11206537
Mar 18 at 9:01













By using enumerate function, we can get index position: [i for i, val in enumerate(item_no) if val in result]

– Pradeep Pandey
Mar 18 at 11:56






By using enumerate function, we can get index position: [i for i, val in enumerate(item_no) if val in result]

– Pradeep Pandey
Mar 18 at 11:56














But isn't max called n times? Not efficient.

– Sachin Dangol
Mar 20 at 1:10





But isn't max called n times? Not efficient.

– Sachin Dangol
Mar 20 at 1:10











1














I think it would be better if we evaluate the max in the array and its count in one iteration



def maxs(iterable):
max = None
count = 0
for index, value in enumerate(iterable):
if index == 0 or value >= max:
if value != max:
count = 0
max = value
count += 1
return count * [max]


print (maxs([5, 6, 7, 8, 8])) # [8, 8]
print (maxs([3, 2, 4, 5, 1, 2, 4, 5, 2, 5, 0])) # [5, 5, 5]
print (maxs([])) # []


Give it a Try!!






share|improve this answer























  • I did! And it worked! That's a very efficient solution.

    – user11206537
    2 days ago











  • @user11206537 Did you measure and compared the efficiency among the solutions. Can you share with us?

    – Grijesh Chauhan
    2 days ago











  • This kind of solution was the one that was actually needed as a part of my school project and my teacher thought of it as a very efficient solution. This code is also written along the lines of my current programming style, which is exactly what I wanted.

    – user11206537
    2 days ago















1














I think it would be better if we evaluate the max in the array and its count in one iteration



def maxs(iterable):
max = None
count = 0
for index, value in enumerate(iterable):
if index == 0 or value >= max:
if value != max:
count = 0
max = value
count += 1
return count * [max]


print (maxs([5, 6, 7, 8, 8])) # [8, 8]
print (maxs([3, 2, 4, 5, 1, 2, 4, 5, 2, 5, 0])) # [5, 5, 5]
print (maxs([])) # []


Give it a Try!!






share|improve this answer























  • I did! And it worked! That's a very efficient solution.

    – user11206537
    2 days ago











  • @user11206537 Did you measure and compared the efficiency among the solutions. Can you share with us?

    – Grijesh Chauhan
    2 days ago











  • This kind of solution was the one that was actually needed as a part of my school project and my teacher thought of it as a very efficient solution. This code is also written along the lines of my current programming style, which is exactly what I wanted.

    – user11206537
    2 days ago













1












1








1







I think it would be better if we evaluate the max in the array and its count in one iteration



def maxs(iterable):
max = None
count = 0
for index, value in enumerate(iterable):
if index == 0 or value >= max:
if value != max:
count = 0
max = value
count += 1
return count * [max]


print (maxs([5, 6, 7, 8, 8])) # [8, 8]
print (maxs([3, 2, 4, 5, 1, 2, 4, 5, 2, 5, 0])) # [5, 5, 5]
print (maxs([])) # []


Give it a Try!!






share|improve this answer













I think it would be better if we evaluate the max in the array and its count in one iteration



def maxs(iterable):
max = None
count = 0
for index, value in enumerate(iterable):
if index == 0 or value >= max:
if value != max:
count = 0
max = value
count += 1
return count * [max]


print (maxs([5, 6, 7, 8, 8])) # [8, 8]
print (maxs([3, 2, 4, 5, 1, 2, 4, 5, 2, 5, 0])) # [5, 5, 5]
print (maxs([])) # []


Give it a Try!!







share|improve this answer












share|improve this answer



share|improve this answer










answered 2 days ago









Grijesh ChauhanGrijesh Chauhan

45.8k1498163




45.8k1498163












  • I did! And it worked! That's a very efficient solution.

    – user11206537
    2 days ago











  • @user11206537 Did you measure and compared the efficiency among the solutions. Can you share with us?

    – Grijesh Chauhan
    2 days ago











  • This kind of solution was the one that was actually needed as a part of my school project and my teacher thought of it as a very efficient solution. This code is also written along the lines of my current programming style, which is exactly what I wanted.

    – user11206537
    2 days ago

















  • I did! And it worked! That's a very efficient solution.

    – user11206537
    2 days ago











  • @user11206537 Did you measure and compared the efficiency among the solutions. Can you share with us?

    – Grijesh Chauhan
    2 days ago











  • This kind of solution was the one that was actually needed as a part of my school project and my teacher thought of it as a very efficient solution. This code is also written along the lines of my current programming style, which is exactly what I wanted.

    – user11206537
    2 days ago
















I did! And it worked! That's a very efficient solution.

– user11206537
2 days ago





I did! And it worked! That's a very efficient solution.

– user11206537
2 days ago













@user11206537 Did you measure and compared the efficiency among the solutions. Can you share with us?

– Grijesh Chauhan
2 days ago





@user11206537 Did you measure and compared the efficiency among the solutions. Can you share with us?

– Grijesh Chauhan
2 days ago













This kind of solution was the one that was actually needed as a part of my school project and my teacher thought of it as a very efficient solution. This code is also written along the lines of my current programming style, which is exactly what I wanted.

– user11206537
2 days ago





This kind of solution was the one that was actually needed as a part of my school project and my teacher thought of it as a very efficient solution. This code is also written along the lines of my current programming style, which is exactly what I wanted.

– user11206537
2 days ago










user11206537 is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















user11206537 is a new contributor. Be nice, and check out our Code of Conduct.












user11206537 is a new contributor. Be nice, and check out our Code of Conduct.











user11206537 is a new contributor. Be nice, and check out our Code of Conduct.














Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55216278%2fhow-to-find-the-largest-numbers-in-a-list-of-elements-possibly-non-unique%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Adding axes to figuresAdding axes labels to LaTeX figuresLaTeX equivalent of ConTeXt buffersRotate a node but not its content: the case of the ellipse decorationHow to define the default vertical distance between nodes?TikZ scaling graphic and adjust node position and keep font sizeNumerical conditional within tikz keys?adding axes to shapesAlign axes across subfiguresAdding figures with a certain orderLine up nested tikz enviroments or how to get rid of themAdding axes labels to LaTeX figures

Tähtien Talli Jäsenet | Lähteet | NavigointivalikkoSuomen Hippos – Tähtien Talli

Do these cracks on my tires look bad? The Next CEO of Stack OverflowDry rot tire should I replace?Having to replace tiresFishtailed so easily? Bad tires? ABS?Filling the tires with something other than air, to avoid puncture hassles?Used Michelin tires safe to install?Do these tyre cracks necessitate replacement?Rumbling noise: tires or mechanicalIs it possible to fix noisy feathered tires?Are bad winter tires still better than summer tires in winter?Torque converter failure - Related to replacing only 2 tires?Why use snow tires on all 4 wheels on 2-wheel-drive cars?