How to find the largest number(s) in a list of elements, possibly non-unique?2019 Community Moderator ElectionHow 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?

Have the tides ever turned twice on any open problem?

How do you justify more code being written by following clean code practices?

Does convergence of polynomials imply that of its coefficients?

Justification failure in beamer enumerate list

What is the tangent at a sharp point on a curve?

Exit shell with shortcut (not typing exit) that closes session properly

Someone scrambled my calling sign- who am I?

Why is indicated airspeed rather than ground speed used during the takeoff roll?

Homology of the fiber

Asserting that Atheism and Theism are both faith based positions

Would this string work as string?

Weird lines in Microsoft Word

What are the rules for concealing thieves' tools (or items in general)?

Should I be concerned about student access to a test bank?

How do researchers send unsolicited emails asking for feedback on their works?

Do I need to convey a moral for each of my blog post?

Friend wants my recommendation but I don't want to

Recursively updating the MLE as new observations stream in

What is the difference between something being completely legal and being completely decriminalized?

Why are there no stars visible in cislunar space?

Why does Surtur say that Thor is Asgard's doom?

Isn't the word "experience" wrongly used in this context?

UK Tourist Visa- Enquiry

Nested Dynamic SOQL Query



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



2019 Community Moderator ElectionHow 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?










13















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.















  • 2





    Band indent at line 8

    – DirtyBit
    18 hours ago






  • 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
    18 hours ago







  • 2





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

    – tripleee
    18 hours ago






  • 1





    @tripleee Indeed. bulls-eye!

    – DirtyBit
    17 hours ago











  • 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
    52 mins ago
















13















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.















  • 2





    Band indent at line 8

    – DirtyBit
    18 hours ago






  • 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
    18 hours ago







  • 2





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

    – tripleee
    18 hours ago






  • 1





    @tripleee Indeed. bulls-eye!

    – DirtyBit
    17 hours ago











  • 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
    52 mins ago














13












13








13


3






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 48 mins ago









smci

15.4k677108




15.4k677108






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 18 hours ago









user11206537user11206537

7512




7512




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.







  • 2





    Band indent at line 8

    – DirtyBit
    18 hours ago






  • 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
    18 hours ago







  • 2





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

    – tripleee
    18 hours ago






  • 1





    @tripleee Indeed. bulls-eye!

    – DirtyBit
    17 hours ago











  • 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
    52 mins ago













  • 2





    Band indent at line 8

    – DirtyBit
    18 hours ago






  • 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
    18 hours ago







  • 2





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

    – tripleee
    18 hours ago






  • 1





    @tripleee Indeed. bulls-eye!

    – DirtyBit
    17 hours ago











  • 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
    52 mins ago








2




2





Band indent at line 8

– DirtyBit
18 hours ago





Band indent at line 8

– DirtyBit
18 hours ago




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
18 hours ago






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
18 hours ago





2




2





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

– tripleee
18 hours ago





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

– tripleee
18 hours ago




1




1





@tripleee Indeed. bulls-eye!

– DirtyBit
17 hours ago





@tripleee Indeed. bulls-eye!

– DirtyBit
17 hours ago













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
52 mins ago






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
52 mins ago













5 Answers
5






active

oldest

votes


















20














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



Note that this last one 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

























  • 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
    17 hours ago






  • 2





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

    – Ev. Kounis
    17 hours ago











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

    – user11206537
    16 hours ago












  • You might want to start with a smaller number than 0.

    – Eric Duminil
    16 hours ago






  • 2





    @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
    15 hours ago


















12














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
    16 hours ago



















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




















  • 2





    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
    17 hours ago












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

    – user11206537
    16 hours ago











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

    – user1717828
    8 hours ago


















2














This issue can be solved in one line, by finding an item which is equal to the maximum value:



[i for i in item_no if i==max(item_no)]






share|improve this answer

























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

    – user11206537
    16 hours ago











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

    – Pradeep Pandey
    13 hours ago



















1














  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
    16 hours 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

























5 Answers
5






active

oldest

votes








5 Answers
5






active

oldest

votes









active

oldest

votes






active

oldest

votes









20














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



Note that this last one 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

























  • 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
    17 hours ago






  • 2





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

    – Ev. Kounis
    17 hours ago











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

    – user11206537
    16 hours ago












  • You might want to start with a smaller number than 0.

    – Eric Duminil
    16 hours ago






  • 2





    @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
    15 hours ago















20














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



Note that this last one 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

























  • 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
    17 hours ago






  • 2





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

    – Ev. Kounis
    17 hours ago











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

    – user11206537
    16 hours ago












  • You might want to start with a smaller number than 0.

    – Eric Duminil
    16 hours ago






  • 2





    @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
    15 hours ago













20












20








20







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



Note that this last one 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















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



Note that this last one 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













share


share








edited 16 hours ago









idmean

10.7k73669




10.7k73669










answered 18 hours ago









Ev. KounisEv. Kounis

11.2k21651




11.2k21651












  • 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
    17 hours ago






  • 2





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

    – Ev. Kounis
    17 hours ago











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

    – user11206537
    16 hours ago












  • You might want to start with a smaller number than 0.

    – Eric Duminil
    16 hours ago






  • 2





    @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
    15 hours ago

















  • 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
    17 hours ago






  • 2





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

    – Ev. Kounis
    17 hours ago











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

    – user11206537
    16 hours ago












  • You might want to start with a smaller number than 0.

    – Eric Duminil
    16 hours ago






  • 2





    @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
    15 hours ago
















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
17 hours ago





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
17 hours ago




2




2





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

– Ev. Kounis
17 hours ago





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

– Ev. Kounis
17 hours ago













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

– user11206537
16 hours ago






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

– user11206537
16 hours ago














You might want to start with a smaller number than 0.

– Eric Duminil
16 hours ago





You might want to start with a smaller number than 0.

– Eric Duminil
16 hours ago




2




2





@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
15 hours ago





@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
15 hours ago













12














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
    16 hours ago
















12














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
    16 hours ago














12












12








12







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 13 hours ago

























answered 17 hours ago









AllanAllan

7,93231534




7,93231534












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

    – user11206537
    16 hours ago


















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

    – user11206537
    16 hours ago

















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

– user11206537
16 hours ago






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

– user11206537
16 hours ago












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




















  • 2





    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
    17 hours ago












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

    – user11206537
    16 hours ago











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

    – user1717828
    8 hours ago















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




















  • 2





    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
    17 hours ago












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

    – user11206537
    16 hours ago











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

    – user1717828
    8 hours ago













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 15 hours ago

























answered 17 hours ago









DaweoDaweo

1,03525




1,03525







  • 2





    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
    17 hours ago












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

    – user11206537
    16 hours ago











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

    – user1717828
    8 hours ago












  • 2





    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
    17 hours ago












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

    – user11206537
    16 hours ago











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

    – user1717828
    8 hours ago







2




2





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
17 hours ago






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
17 hours ago














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

– user11206537
16 hours ago





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

– user11206537
16 hours ago













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

– user1717828
8 hours ago





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

– user1717828
8 hours ago











2














This issue can be solved in one line, by finding an item which is equal to the maximum value:



[i for i in item_no if i==max(item_no)]






share|improve this answer

























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

    – user11206537
    16 hours ago











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

    – Pradeep Pandey
    13 hours ago
















2














This issue can be solved in one line, by finding an item which is equal to the maximum value:



[i for i in item_no if i==max(item_no)]






share|improve this answer

























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

    – user11206537
    16 hours ago











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

    – Pradeep Pandey
    13 hours ago














2












2








2







This issue can be solved in one line, by finding an item which is equal to the maximum value:



[i for i in item_no if i==max(item_no)]






share|improve this answer















This issue can be solved in one line, by finding an item which is equal to the maximum value:



[i for i in item_no if i==max(item_no)]







share|improve this answer














share|improve this answer



share|improve this answer








edited 16 hours ago









376writer

132




132










answered 17 hours ago









Pradeep PandeyPradeep Pandey

15417




15417












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

    – user11206537
    16 hours ago











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

    – Pradeep Pandey
    13 hours ago


















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

    – user11206537
    16 hours ago











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

    – Pradeep Pandey
    13 hours ago

















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

– user11206537
16 hours ago





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

– user11206537
16 hours ago













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

– Pradeep Pandey
13 hours ago






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

– Pradeep Pandey
13 hours ago












1














  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
    16 hours ago















1














  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
    16 hours ago













1












1








1







  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 17 hours ago









DirtyBitDirtyBit

9,05821540




9,05821540












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

    – user11206537
    16 hours ago

















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

    – user11206537
    16 hours ago
















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

– user11206537
16 hours ago





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

– user11206537
16 hours 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

名間水力發電廠 目录 沿革 設施 鄰近設施 註釋 外部連結 导航菜单23°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.7113923°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.71139計畫概要原始内容臺灣第一座BOT 模式開發的水力發電廠-名間水力電廠名間水力發電廠 水利署首件BOT案原始内容《小檔案》名間電廠 首座BOT水力發電廠原始内容名間電廠BOT - 經濟部水利署中區水資源局

Prove that NP is closed under karp reduction?Space(n) not closed under Karp reductions - what about NTime(n)?Class P is closed under rotation?Prove or disprove that $NL$ is closed under polynomial many-one reductions$mathbfNC_2$ is closed under log-space reductionOn Karp reductionwhen can I know if a class (complexity) is closed under reduction (cook/karp)Check if class $PSPACE$ is closed under polyonomially space reductionIs NPSPACE also closed under polynomial-time reduction and under log-space reduction?Prove PSPACE is closed under complement?Prove PSPACE is closed under union?

Is my guitar’s action too high? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)Strings too stiff on a recently purchased acoustic guitar | Cort AD880CEIs the action of my guitar really high?Μy little finger is too weak to play guitarWith guitar, how long should I give my fingers to strengthen / callous?When playing a fret the guitar sounds mutedPlaying (Barre) chords up the guitar neckI think my guitar strings are wound too tight and I can't play barre chordsF barre chord on an SG guitarHow to find to the right strings of a barre chord by feel?High action on higher fret on my steel acoustic guitar