Replace min & max value in a list












-3














I need help to write a python function for lists that will do two things.



First instance, it will replace the largest and smallest elements in the list with the number 5000 (for largest) and -5000 (for smallest).



thelist = input("Please input a list with numbers: ")
mylist = list(map(int, thelist.split()))


Please input a list with numbers: 1 2 3 4 5 6 7 8 9 10



print(mylist)


Result: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]



Honestly I have no clue how to replace the 1 and 10 with the numbers 5000 and -5000 using a py function. I can get the min and max number but replacing them is something I don't know how to do.










share|improve this question




















  • 1




    mylist[0] and mylist[10] will let you access the first and last members of the list. len(mylist) will give you the length of the list.
    – Soumya Kanti
    Nov 20 at 7:20
















-3














I need help to write a python function for lists that will do two things.



First instance, it will replace the largest and smallest elements in the list with the number 5000 (for largest) and -5000 (for smallest).



thelist = input("Please input a list with numbers: ")
mylist = list(map(int, thelist.split()))


Please input a list with numbers: 1 2 3 4 5 6 7 8 9 10



print(mylist)


Result: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]



Honestly I have no clue how to replace the 1 and 10 with the numbers 5000 and -5000 using a py function. I can get the min and max number but replacing them is something I don't know how to do.










share|improve this question




















  • 1




    mylist[0] and mylist[10] will let you access the first and last members of the list. len(mylist) will give you the length of the list.
    – Soumya Kanti
    Nov 20 at 7:20














-3












-3








-3







I need help to write a python function for lists that will do two things.



First instance, it will replace the largest and smallest elements in the list with the number 5000 (for largest) and -5000 (for smallest).



thelist = input("Please input a list with numbers: ")
mylist = list(map(int, thelist.split()))


Please input a list with numbers: 1 2 3 4 5 6 7 8 9 10



print(mylist)


Result: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]



Honestly I have no clue how to replace the 1 and 10 with the numbers 5000 and -5000 using a py function. I can get the min and max number but replacing them is something I don't know how to do.










share|improve this question















I need help to write a python function for lists that will do two things.



First instance, it will replace the largest and smallest elements in the list with the number 5000 (for largest) and -5000 (for smallest).



thelist = input("Please input a list with numbers: ")
mylist = list(map(int, thelist.split()))


Please input a list with numbers: 1 2 3 4 5 6 7 8 9 10



print(mylist)


Result: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]



Honestly I have no clue how to replace the 1 and 10 with the numbers 5000 and -5000 using a py function. I can get the min and max number but replacing them is something I don't know how to do.







python list






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 8:16









Elisha

19.5k45069




19.5k45069










asked Nov 20 at 7:15









blargh

13




13








  • 1




    mylist[0] and mylist[10] will let you access the first and last members of the list. len(mylist) will give you the length of the list.
    – Soumya Kanti
    Nov 20 at 7:20














  • 1




    mylist[0] and mylist[10] will let you access the first and last members of the list. len(mylist) will give you the length of the list.
    – Soumya Kanti
    Nov 20 at 7:20








1




1




mylist[0] and mylist[10] will let you access the first and last members of the list. len(mylist) will give you the length of the list.
– Soumya Kanti
Nov 20 at 7:20




mylist[0] and mylist[10] will let you access the first and last members of the list. len(mylist) will give you the length of the list.
– Soumya Kanti
Nov 20 at 7:20












5 Answers
5






active

oldest

votes


















1














In a bit condensed format using list comprehension, You can use



a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
b = [-5000 if x == min(a) else 5000 if x == max(a) else x for x in a]


It replaces the minimal elements by -5000 and the maximal ones by 5000. f there are several minimal values, all of them will be replaced. You can use a function to use other values than -5000, 5000.






share|improve this answer





























    1














    You can use mylist[0] = -5000 and mylist[-1] = 5000.



    The negative index counts from the right side of the list



    Or if your list is jumbled



    Try this,



    min_pos = mylist.index(min(mylist))
    max_pos = mylist.index(max(mylist))

    mylist[min_pos] = -5000
    mylist[max_pos] = 5000

    print(mylist)


    Hope this answers your question






    share|improve this answer





























      0














      mylist[mylist.index(min(mylist))] = -5000
      mylist[mylist.index(max(mylist))] = 5000





      share|improve this answer





















      • Thank you very much Rudolf! I created a function that utilized this and it replaced the min/max with the -5000 and 5000. Thank you very much! I will further study this index feature, had no clue it was even a thing!
        – blargh
        Nov 20 at 7:30










      • Glad I could help you)
        – Rudolf Morkovskyi
        Nov 20 at 7:34



















      0














      This can be done by two phases over min/max:



      arr = [1,2,3,4]
      arr[arr.index(max(arr))] = 5000


      This yields:




      [1, 2, 3, 5000]




      The two phases here are:




      1. Find the max value: max(arr). In this example, the value will be 4.

      2. Find the index of max value: arr.index(4). In this example, the index will be 3.


      The two phases lead to assignment at the index at the max value cell. Note that this assumes a unique max value.






      share|improve this answer































        0














        You can have a function like this:



        In [361]: def replace_vals(l):
        ...: min_index = l.index(min(l)) # min(l) finds minimum of list. 'l.index()' finds the index of a given value.
        ...: max_index = l.index(max(l)) # max(l) finds maximum of list.
        ...: l[min_index] = -5000 # Just replace min_index found above by -5000
        ...: l[max_index] = 5000 # Just replace max_index found above by 5000
        ...:
        In [355]: lst=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

        In [362]: replace_vals(lst)

        In [363]: lst
        Out[363]: [-5000, 2, 3, 4, 5, 6, 7, 8, 9, 5000]





        share|improve this answer























          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
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387996%2freplace-min-max-value-in-a-list%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









          1














          In a bit condensed format using list comprehension, You can use



          a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
          b = [-5000 if x == min(a) else 5000 if x == max(a) else x for x in a]


          It replaces the minimal elements by -5000 and the maximal ones by 5000. f there are several minimal values, all of them will be replaced. You can use a function to use other values than -5000, 5000.






          share|improve this answer


























            1














            In a bit condensed format using list comprehension, You can use



            a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
            b = [-5000 if x == min(a) else 5000 if x == max(a) else x for x in a]


            It replaces the minimal elements by -5000 and the maximal ones by 5000. f there are several minimal values, all of them will be replaced. You can use a function to use other values than -5000, 5000.






            share|improve this answer
























              1












              1








              1






              In a bit condensed format using list comprehension, You can use



              a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
              b = [-5000 if x == min(a) else 5000 if x == max(a) else x for x in a]


              It replaces the minimal elements by -5000 and the maximal ones by 5000. f there are several minimal values, all of them will be replaced. You can use a function to use other values than -5000, 5000.






              share|improve this answer












              In a bit condensed format using list comprehension, You can use



              a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
              b = [-5000 if x == min(a) else 5000 if x == max(a) else x for x in a]


              It replaces the minimal elements by -5000 and the maximal ones by 5000. f there are several minimal values, all of them will be replaced. You can use a function to use other values than -5000, 5000.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 20 at 7:24









              Sam The Sid

              216




              216

























                  1














                  You can use mylist[0] = -5000 and mylist[-1] = 5000.



                  The negative index counts from the right side of the list



                  Or if your list is jumbled



                  Try this,



                  min_pos = mylist.index(min(mylist))
                  max_pos = mylist.index(max(mylist))

                  mylist[min_pos] = -5000
                  mylist[max_pos] = 5000

                  print(mylist)


                  Hope this answers your question






                  share|improve this answer


























                    1














                    You can use mylist[0] = -5000 and mylist[-1] = 5000.



                    The negative index counts from the right side of the list



                    Or if your list is jumbled



                    Try this,



                    min_pos = mylist.index(min(mylist))
                    max_pos = mylist.index(max(mylist))

                    mylist[min_pos] = -5000
                    mylist[max_pos] = 5000

                    print(mylist)


                    Hope this answers your question






                    share|improve this answer
























                      1












                      1








                      1






                      You can use mylist[0] = -5000 and mylist[-1] = 5000.



                      The negative index counts from the right side of the list



                      Or if your list is jumbled



                      Try this,



                      min_pos = mylist.index(min(mylist))
                      max_pos = mylist.index(max(mylist))

                      mylist[min_pos] = -5000
                      mylist[max_pos] = 5000

                      print(mylist)


                      Hope this answers your question






                      share|improve this answer












                      You can use mylist[0] = -5000 and mylist[-1] = 5000.



                      The negative index counts from the right side of the list



                      Or if your list is jumbled



                      Try this,



                      min_pos = mylist.index(min(mylist))
                      max_pos = mylist.index(max(mylist))

                      mylist[min_pos] = -5000
                      mylist[max_pos] = 5000

                      print(mylist)


                      Hope this answers your question







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 20 at 7:31









                      Avichal Bettoli

                      493




                      493























                          0














                          mylist[mylist.index(min(mylist))] = -5000
                          mylist[mylist.index(max(mylist))] = 5000





                          share|improve this answer





















                          • Thank you very much Rudolf! I created a function that utilized this and it replaced the min/max with the -5000 and 5000. Thank you very much! I will further study this index feature, had no clue it was even a thing!
                            – blargh
                            Nov 20 at 7:30










                          • Glad I could help you)
                            – Rudolf Morkovskyi
                            Nov 20 at 7:34
















                          0














                          mylist[mylist.index(min(mylist))] = -5000
                          mylist[mylist.index(max(mylist))] = 5000





                          share|improve this answer





















                          • Thank you very much Rudolf! I created a function that utilized this and it replaced the min/max with the -5000 and 5000. Thank you very much! I will further study this index feature, had no clue it was even a thing!
                            – blargh
                            Nov 20 at 7:30










                          • Glad I could help you)
                            – Rudolf Morkovskyi
                            Nov 20 at 7:34














                          0












                          0








                          0






                          mylist[mylist.index(min(mylist))] = -5000
                          mylist[mylist.index(max(mylist))] = 5000





                          share|improve this answer












                          mylist[mylist.index(min(mylist))] = -5000
                          mylist[mylist.index(max(mylist))] = 5000






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 20 at 7:22









                          Rudolf Morkovskyi

                          720117




                          720117












                          • Thank you very much Rudolf! I created a function that utilized this and it replaced the min/max with the -5000 and 5000. Thank you very much! I will further study this index feature, had no clue it was even a thing!
                            – blargh
                            Nov 20 at 7:30










                          • Glad I could help you)
                            – Rudolf Morkovskyi
                            Nov 20 at 7:34


















                          • Thank you very much Rudolf! I created a function that utilized this and it replaced the min/max with the -5000 and 5000. Thank you very much! I will further study this index feature, had no clue it was even a thing!
                            – blargh
                            Nov 20 at 7:30










                          • Glad I could help you)
                            – Rudolf Morkovskyi
                            Nov 20 at 7:34
















                          Thank you very much Rudolf! I created a function that utilized this and it replaced the min/max with the -5000 and 5000. Thank you very much! I will further study this index feature, had no clue it was even a thing!
                          – blargh
                          Nov 20 at 7:30




                          Thank you very much Rudolf! I created a function that utilized this and it replaced the min/max with the -5000 and 5000. Thank you very much! I will further study this index feature, had no clue it was even a thing!
                          – blargh
                          Nov 20 at 7:30












                          Glad I could help you)
                          – Rudolf Morkovskyi
                          Nov 20 at 7:34




                          Glad I could help you)
                          – Rudolf Morkovskyi
                          Nov 20 at 7:34











                          0














                          This can be done by two phases over min/max:



                          arr = [1,2,3,4]
                          arr[arr.index(max(arr))] = 5000


                          This yields:




                          [1, 2, 3, 5000]




                          The two phases here are:




                          1. Find the max value: max(arr). In this example, the value will be 4.

                          2. Find the index of max value: arr.index(4). In this example, the index will be 3.


                          The two phases lead to assignment at the index at the max value cell. Note that this assumes a unique max value.






                          share|improve this answer




























                            0














                            This can be done by two phases over min/max:



                            arr = [1,2,3,4]
                            arr[arr.index(max(arr))] = 5000


                            This yields:




                            [1, 2, 3, 5000]




                            The two phases here are:




                            1. Find the max value: max(arr). In this example, the value will be 4.

                            2. Find the index of max value: arr.index(4). In this example, the index will be 3.


                            The two phases lead to assignment at the index at the max value cell. Note that this assumes a unique max value.






                            share|improve this answer


























                              0












                              0








                              0






                              This can be done by two phases over min/max:



                              arr = [1,2,3,4]
                              arr[arr.index(max(arr))] = 5000


                              This yields:




                              [1, 2, 3, 5000]




                              The two phases here are:




                              1. Find the max value: max(arr). In this example, the value will be 4.

                              2. Find the index of max value: arr.index(4). In this example, the index will be 3.


                              The two phases lead to assignment at the index at the max value cell. Note that this assumes a unique max value.






                              share|improve this answer














                              This can be done by two phases over min/max:



                              arr = [1,2,3,4]
                              arr[arr.index(max(arr))] = 5000


                              This yields:




                              [1, 2, 3, 5000]




                              The two phases here are:




                              1. Find the max value: max(arr). In this example, the value will be 4.

                              2. Find the index of max value: arr.index(4). In this example, the index will be 3.


                              The two phases lead to assignment at the index at the max value cell. Note that this assumes a unique max value.







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Nov 20 at 7:30

























                              answered Nov 20 at 7:21









                              Elisha

                              19.5k45069




                              19.5k45069























                                  0














                                  You can have a function like this:



                                  In [361]: def replace_vals(l):
                                  ...: min_index = l.index(min(l)) # min(l) finds minimum of list. 'l.index()' finds the index of a given value.
                                  ...: max_index = l.index(max(l)) # max(l) finds maximum of list.
                                  ...: l[min_index] = -5000 # Just replace min_index found above by -5000
                                  ...: l[max_index] = 5000 # Just replace max_index found above by 5000
                                  ...:
                                  In [355]: lst=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

                                  In [362]: replace_vals(lst)

                                  In [363]: lst
                                  Out[363]: [-5000, 2, 3, 4, 5, 6, 7, 8, 9, 5000]





                                  share|improve this answer




























                                    0














                                    You can have a function like this:



                                    In [361]: def replace_vals(l):
                                    ...: min_index = l.index(min(l)) # min(l) finds minimum of list. 'l.index()' finds the index of a given value.
                                    ...: max_index = l.index(max(l)) # max(l) finds maximum of list.
                                    ...: l[min_index] = -5000 # Just replace min_index found above by -5000
                                    ...: l[max_index] = 5000 # Just replace max_index found above by 5000
                                    ...:
                                    In [355]: lst=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

                                    In [362]: replace_vals(lst)

                                    In [363]: lst
                                    Out[363]: [-5000, 2, 3, 4, 5, 6, 7, 8, 9, 5000]





                                    share|improve this answer


























                                      0












                                      0








                                      0






                                      You can have a function like this:



                                      In [361]: def replace_vals(l):
                                      ...: min_index = l.index(min(l)) # min(l) finds minimum of list. 'l.index()' finds the index of a given value.
                                      ...: max_index = l.index(max(l)) # max(l) finds maximum of list.
                                      ...: l[min_index] = -5000 # Just replace min_index found above by -5000
                                      ...: l[max_index] = 5000 # Just replace max_index found above by 5000
                                      ...:
                                      In [355]: lst=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

                                      In [362]: replace_vals(lst)

                                      In [363]: lst
                                      Out[363]: [-5000, 2, 3, 4, 5, 6, 7, 8, 9, 5000]





                                      share|improve this answer














                                      You can have a function like this:



                                      In [361]: def replace_vals(l):
                                      ...: min_index = l.index(min(l)) # min(l) finds minimum of list. 'l.index()' finds the index of a given value.
                                      ...: max_index = l.index(max(l)) # max(l) finds maximum of list.
                                      ...: l[min_index] = -5000 # Just replace min_index found above by -5000
                                      ...: l[max_index] = 5000 # Just replace max_index found above by 5000
                                      ...:
                                      In [355]: lst=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

                                      In [362]: replace_vals(lst)

                                      In [363]: lst
                                      Out[363]: [-5000, 2, 3, 4, 5, 6, 7, 8, 9, 5000]






                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Nov 20 at 7:31

























                                      answered Nov 20 at 7:22









                                      Mayank Porwal

                                      4,4541623




                                      4,4541623






























                                          draft saved

                                          draft discarded




















































                                          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.





                                          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                          Please pay close attention to the following guidance:


                                          • 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%2f53387996%2freplace-min-max-value-in-a-list%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

                                          "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                                          Alcedinidae

                                          RAC Tourist Trophy