Compiling a nested list with conditional statement










1















I want to create a nested list with one or more of the below lists of tuples, with the order of the list based on user's preference.



Fruits=[("Apples",2),("Oranges",3),("Pineapples",5)]
Clothes=[("Jeans",10),("Shirts",5),("Dresses",15)]
Pets=[("Dogs",3),("Cats",4),("Turtles",2)]


The order of the nested list will depend on the preference of the user.For instance, if the user prefer pets over clothes over fruits. The list will look like this:



[[("Jeans",10),("Shirts",5),("Dresses",15)],[("Jeans",10),("Shirts",5), 
("Dresses",15)],[("Apples",2),("Oranges",3),("Pineapples",5)]]


The user also have the option of picking only one or two items. For instance, if the user only cares about pet and then clothing (doesn't care about fruits), his/her list will look like this.



[[("Dogs",3),("Cats",4),("Turtles",2)],[("Jeans",10),("Shirts",5), 
("Dresses",15)]]


The user input is a list with the preferences in order. For example:



preference= ["Pets", "Fruits", "Clothing"] # preference list for users who care about pets over fruits over clothing.
or
preference= ["Fruits", "Clothing"] # preference list for users who care about fruits over clothing (no regard for pets)


This is how I've tried to tackle the problem. First I create an empty list with a corresponding number of nested list:



empty_list=[ for x in range (len(preferences)]


This creates a place holder for the number of nested list I need. I then run a bunch of conditional statement to pop in one list at the time:



if preference[0]=="Fruits":
empty_list[0]=Fruits
if preference[1]=="Clothes":
empty_list[1]=Clothes
empty_list[2]=Pets
elif preference[1]=="Pets":
empty_list[1]=Pets
empty_list[2]=Clothes

if preference[0]=="Pets":
empty_list[0]=Pets
if preference[1]=="Clothes":
empty_list[1]=Clothes
empty_list[2]=Fruits
elif preference[1]=="Fruits":
empty_list[1]=Fruits
empty_list[2]=Clothes

if preference[0]=="Clothes":
empty_list[0]=Clothes
if preference[1]=="Pets":
empty_list[1]=Pets
empty_list[2]=Fruits
elif preference[1]=="Fruits":
empty_list[1]=Fruits
empty_list[2]=Pets


My solution is inefficient and also causes problem with list assignment out of range if there are only two preference as opposed to three. Is there a more Pythonic way of writing this?



Any tip or guidance is most appreciated.










share|improve this question


























    1















    I want to create a nested list with one or more of the below lists of tuples, with the order of the list based on user's preference.



    Fruits=[("Apples",2),("Oranges",3),("Pineapples",5)]
    Clothes=[("Jeans",10),("Shirts",5),("Dresses",15)]
    Pets=[("Dogs",3),("Cats",4),("Turtles",2)]


    The order of the nested list will depend on the preference of the user.For instance, if the user prefer pets over clothes over fruits. The list will look like this:



    [[("Jeans",10),("Shirts",5),("Dresses",15)],[("Jeans",10),("Shirts",5), 
    ("Dresses",15)],[("Apples",2),("Oranges",3),("Pineapples",5)]]


    The user also have the option of picking only one or two items. For instance, if the user only cares about pet and then clothing (doesn't care about fruits), his/her list will look like this.



    [[("Dogs",3),("Cats",4),("Turtles",2)],[("Jeans",10),("Shirts",5), 
    ("Dresses",15)]]


    The user input is a list with the preferences in order. For example:



    preference= ["Pets", "Fruits", "Clothing"] # preference list for users who care about pets over fruits over clothing.
    or
    preference= ["Fruits", "Clothing"] # preference list for users who care about fruits over clothing (no regard for pets)


    This is how I've tried to tackle the problem. First I create an empty list with a corresponding number of nested list:



    empty_list=[ for x in range (len(preferences)]


    This creates a place holder for the number of nested list I need. I then run a bunch of conditional statement to pop in one list at the time:



    if preference[0]=="Fruits":
    empty_list[0]=Fruits
    if preference[1]=="Clothes":
    empty_list[1]=Clothes
    empty_list[2]=Pets
    elif preference[1]=="Pets":
    empty_list[1]=Pets
    empty_list[2]=Clothes

    if preference[0]=="Pets":
    empty_list[0]=Pets
    if preference[1]=="Clothes":
    empty_list[1]=Clothes
    empty_list[2]=Fruits
    elif preference[1]=="Fruits":
    empty_list[1]=Fruits
    empty_list[2]=Clothes

    if preference[0]=="Clothes":
    empty_list[0]=Clothes
    if preference[1]=="Pets":
    empty_list[1]=Pets
    empty_list[2]=Fruits
    elif preference[1]=="Fruits":
    empty_list[1]=Fruits
    empty_list[2]=Pets


    My solution is inefficient and also causes problem with list assignment out of range if there are only two preference as opposed to three. Is there a more Pythonic way of writing this?



    Any tip or guidance is most appreciated.










    share|improve this question
























      1












      1








      1








      I want to create a nested list with one or more of the below lists of tuples, with the order of the list based on user's preference.



      Fruits=[("Apples",2),("Oranges",3),("Pineapples",5)]
      Clothes=[("Jeans",10),("Shirts",5),("Dresses",15)]
      Pets=[("Dogs",3),("Cats",4),("Turtles",2)]


      The order of the nested list will depend on the preference of the user.For instance, if the user prefer pets over clothes over fruits. The list will look like this:



      [[("Jeans",10),("Shirts",5),("Dresses",15)],[("Jeans",10),("Shirts",5), 
      ("Dresses",15)],[("Apples",2),("Oranges",3),("Pineapples",5)]]


      The user also have the option of picking only one or two items. For instance, if the user only cares about pet and then clothing (doesn't care about fruits), his/her list will look like this.



      [[("Dogs",3),("Cats",4),("Turtles",2)],[("Jeans",10),("Shirts",5), 
      ("Dresses",15)]]


      The user input is a list with the preferences in order. For example:



      preference= ["Pets", "Fruits", "Clothing"] # preference list for users who care about pets over fruits over clothing.
      or
      preference= ["Fruits", "Clothing"] # preference list for users who care about fruits over clothing (no regard for pets)


      This is how I've tried to tackle the problem. First I create an empty list with a corresponding number of nested list:



      empty_list=[ for x in range (len(preferences)]


      This creates a place holder for the number of nested list I need. I then run a bunch of conditional statement to pop in one list at the time:



      if preference[0]=="Fruits":
      empty_list[0]=Fruits
      if preference[1]=="Clothes":
      empty_list[1]=Clothes
      empty_list[2]=Pets
      elif preference[1]=="Pets":
      empty_list[1]=Pets
      empty_list[2]=Clothes

      if preference[0]=="Pets":
      empty_list[0]=Pets
      if preference[1]=="Clothes":
      empty_list[1]=Clothes
      empty_list[2]=Fruits
      elif preference[1]=="Fruits":
      empty_list[1]=Fruits
      empty_list[2]=Clothes

      if preference[0]=="Clothes":
      empty_list[0]=Clothes
      if preference[1]=="Pets":
      empty_list[1]=Pets
      empty_list[2]=Fruits
      elif preference[1]=="Fruits":
      empty_list[1]=Fruits
      empty_list[2]=Pets


      My solution is inefficient and also causes problem with list assignment out of range if there are only two preference as opposed to three. Is there a more Pythonic way of writing this?



      Any tip or guidance is most appreciated.










      share|improve this question














      I want to create a nested list with one or more of the below lists of tuples, with the order of the list based on user's preference.



      Fruits=[("Apples",2),("Oranges",3),("Pineapples",5)]
      Clothes=[("Jeans",10),("Shirts",5),("Dresses",15)]
      Pets=[("Dogs",3),("Cats",4),("Turtles",2)]


      The order of the nested list will depend on the preference of the user.For instance, if the user prefer pets over clothes over fruits. The list will look like this:



      [[("Jeans",10),("Shirts",5),("Dresses",15)],[("Jeans",10),("Shirts",5), 
      ("Dresses",15)],[("Apples",2),("Oranges",3),("Pineapples",5)]]


      The user also have the option of picking only one or two items. For instance, if the user only cares about pet and then clothing (doesn't care about fruits), his/her list will look like this.



      [[("Dogs",3),("Cats",4),("Turtles",2)],[("Jeans",10),("Shirts",5), 
      ("Dresses",15)]]


      The user input is a list with the preferences in order. For example:



      preference= ["Pets", "Fruits", "Clothing"] # preference list for users who care about pets over fruits over clothing.
      or
      preference= ["Fruits", "Clothing"] # preference list for users who care about fruits over clothing (no regard for pets)


      This is how I've tried to tackle the problem. First I create an empty list with a corresponding number of nested list:



      empty_list=[ for x in range (len(preferences)]


      This creates a place holder for the number of nested list I need. I then run a bunch of conditional statement to pop in one list at the time:



      if preference[0]=="Fruits":
      empty_list[0]=Fruits
      if preference[1]=="Clothes":
      empty_list[1]=Clothes
      empty_list[2]=Pets
      elif preference[1]=="Pets":
      empty_list[1]=Pets
      empty_list[2]=Clothes

      if preference[0]=="Pets":
      empty_list[0]=Pets
      if preference[1]=="Clothes":
      empty_list[1]=Clothes
      empty_list[2]=Fruits
      elif preference[1]=="Fruits":
      empty_list[1]=Fruits
      empty_list[2]=Clothes

      if preference[0]=="Clothes":
      empty_list[0]=Clothes
      if preference[1]=="Pets":
      empty_list[1]=Pets
      empty_list[2]=Fruits
      elif preference[1]=="Fruits":
      empty_list[1]=Fruits
      empty_list[2]=Pets


      My solution is inefficient and also causes problem with list assignment out of range if there are only two preference as opposed to three. Is there a more Pythonic way of writing this?



      Any tip or guidance is most appreciated.







      python






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 '18 at 23:57









      sangurocactussangurocactus

      112




      112






















          2 Answers
          2






          active

          oldest

          votes


















          1














          you should store your data in a dictionary



          and then access that dictionary by key



          data = dict(
          Fruits=[("Apples",2),("Oranges",3),("Pineapples",5)]
          Clothes=[("Jeans",10),("Shirts",5),("Dresses",15)]
          Pets=[("Dogs",3),("Cats",4),("Turtles",2)]
          )


          once you have it in a dict you can easily access the values with variables



          fruits = "Fruits"
          my_fruits = data[fruits]


          you can then use a simple list comprehension to capture all of your interests



          interests = ["Fruits","Pets"] 
          interesting_things = [data[interest] for interest in interests]

          interests = ["Fruits","Clothes"]
          interesting_things = [data[interest] for interest in interests]





          share|improve this answer
































            0














            Try this method.



            def prefer(preference):
            preference_list =

            choices =
            "fruits": [
            ("Apples", 2),
            ("Oranges", 3),
            ("Pineapples", 5)
            ],
            "cloths": [
            ("Jeans", 10),
            ("Shirts", 5),
            ("Dresses", 15)
            ],
            "pets": [
            ("Dogs", 3),
            ("Cats", 4),
            ("Turtles", 2)
            ]


            for choice in choices:
            if choice in preference:
            preference_list.insert(preference.index(choice), choices[choice])

            else:
            preference_list.append(choices[choice])


            print(preference_list)

            prefer(("fruits", "cloths"))





            share|improve this answer























            • This works great. Thank you! The only change I had to make was getting rid of the preference_list.append (choices[choice]) as the number of preference list can be fewer than 3.

              – sangurocactus
              Nov 14 '18 at 6:13










            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%2f53291213%2fcompiling-a-nested-list-with-conditional-statement%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            you should store your data in a dictionary



            and then access that dictionary by key



            data = dict(
            Fruits=[("Apples",2),("Oranges",3),("Pineapples",5)]
            Clothes=[("Jeans",10),("Shirts",5),("Dresses",15)]
            Pets=[("Dogs",3),("Cats",4),("Turtles",2)]
            )


            once you have it in a dict you can easily access the values with variables



            fruits = "Fruits"
            my_fruits = data[fruits]


            you can then use a simple list comprehension to capture all of your interests



            interests = ["Fruits","Pets"] 
            interesting_things = [data[interest] for interest in interests]

            interests = ["Fruits","Clothes"]
            interesting_things = [data[interest] for interest in interests]





            share|improve this answer





























              1














              you should store your data in a dictionary



              and then access that dictionary by key



              data = dict(
              Fruits=[("Apples",2),("Oranges",3),("Pineapples",5)]
              Clothes=[("Jeans",10),("Shirts",5),("Dresses",15)]
              Pets=[("Dogs",3),("Cats",4),("Turtles",2)]
              )


              once you have it in a dict you can easily access the values with variables



              fruits = "Fruits"
              my_fruits = data[fruits]


              you can then use a simple list comprehension to capture all of your interests



              interests = ["Fruits","Pets"] 
              interesting_things = [data[interest] for interest in interests]

              interests = ["Fruits","Clothes"]
              interesting_things = [data[interest] for interest in interests]





              share|improve this answer



























                1












                1








                1







                you should store your data in a dictionary



                and then access that dictionary by key



                data = dict(
                Fruits=[("Apples",2),("Oranges",3),("Pineapples",5)]
                Clothes=[("Jeans",10),("Shirts",5),("Dresses",15)]
                Pets=[("Dogs",3),("Cats",4),("Turtles",2)]
                )


                once you have it in a dict you can easily access the values with variables



                fruits = "Fruits"
                my_fruits = data[fruits]


                you can then use a simple list comprehension to capture all of your interests



                interests = ["Fruits","Pets"] 
                interesting_things = [data[interest] for interest in interests]

                interests = ["Fruits","Clothes"]
                interesting_things = [data[interest] for interest in interests]





                share|improve this answer















                you should store your data in a dictionary



                and then access that dictionary by key



                data = dict(
                Fruits=[("Apples",2),("Oranges",3),("Pineapples",5)]
                Clothes=[("Jeans",10),("Shirts",5),("Dresses",15)]
                Pets=[("Dogs",3),("Cats",4),("Turtles",2)]
                )


                once you have it in a dict you can easily access the values with variables



                fruits = "Fruits"
                my_fruits = data[fruits]


                you can then use a simple list comprehension to capture all of your interests



                interests = ["Fruits","Pets"] 
                interesting_things = [data[interest] for interest in interests]

                interests = ["Fruits","Clothes"]
                interesting_things = [data[interest] for interest in interests]






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 14 '18 at 0:24

























                answered Nov 14 '18 at 0:13









                Joran BeasleyJoran Beasley

                73k678118




                73k678118























                    0














                    Try this method.



                    def prefer(preference):
                    preference_list =

                    choices =
                    "fruits": [
                    ("Apples", 2),
                    ("Oranges", 3),
                    ("Pineapples", 5)
                    ],
                    "cloths": [
                    ("Jeans", 10),
                    ("Shirts", 5),
                    ("Dresses", 15)
                    ],
                    "pets": [
                    ("Dogs", 3),
                    ("Cats", 4),
                    ("Turtles", 2)
                    ]


                    for choice in choices:
                    if choice in preference:
                    preference_list.insert(preference.index(choice), choices[choice])

                    else:
                    preference_list.append(choices[choice])


                    print(preference_list)

                    prefer(("fruits", "cloths"))





                    share|improve this answer























                    • This works great. Thank you! The only change I had to make was getting rid of the preference_list.append (choices[choice]) as the number of preference list can be fewer than 3.

                      – sangurocactus
                      Nov 14 '18 at 6:13















                    0














                    Try this method.



                    def prefer(preference):
                    preference_list =

                    choices =
                    "fruits": [
                    ("Apples", 2),
                    ("Oranges", 3),
                    ("Pineapples", 5)
                    ],
                    "cloths": [
                    ("Jeans", 10),
                    ("Shirts", 5),
                    ("Dresses", 15)
                    ],
                    "pets": [
                    ("Dogs", 3),
                    ("Cats", 4),
                    ("Turtles", 2)
                    ]


                    for choice in choices:
                    if choice in preference:
                    preference_list.insert(preference.index(choice), choices[choice])

                    else:
                    preference_list.append(choices[choice])


                    print(preference_list)

                    prefer(("fruits", "cloths"))





                    share|improve this answer























                    • This works great. Thank you! The only change I had to make was getting rid of the preference_list.append (choices[choice]) as the number of preference list can be fewer than 3.

                      – sangurocactus
                      Nov 14 '18 at 6:13













                    0












                    0








                    0







                    Try this method.



                    def prefer(preference):
                    preference_list =

                    choices =
                    "fruits": [
                    ("Apples", 2),
                    ("Oranges", 3),
                    ("Pineapples", 5)
                    ],
                    "cloths": [
                    ("Jeans", 10),
                    ("Shirts", 5),
                    ("Dresses", 15)
                    ],
                    "pets": [
                    ("Dogs", 3),
                    ("Cats", 4),
                    ("Turtles", 2)
                    ]


                    for choice in choices:
                    if choice in preference:
                    preference_list.insert(preference.index(choice), choices[choice])

                    else:
                    preference_list.append(choices[choice])


                    print(preference_list)

                    prefer(("fruits", "cloths"))





                    share|improve this answer













                    Try this method.



                    def prefer(preference):
                    preference_list =

                    choices =
                    "fruits": [
                    ("Apples", 2),
                    ("Oranges", 3),
                    ("Pineapples", 5)
                    ],
                    "cloths": [
                    ("Jeans", 10),
                    ("Shirts", 5),
                    ("Dresses", 15)
                    ],
                    "pets": [
                    ("Dogs", 3),
                    ("Cats", 4),
                    ("Turtles", 2)
                    ]


                    for choice in choices:
                    if choice in preference:
                    preference_list.insert(preference.index(choice), choices[choice])

                    else:
                    preference_list.append(choices[choice])


                    print(preference_list)

                    prefer(("fruits", "cloths"))






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 14 '18 at 0:22









                    BaryonBaryon

                    365




                    365












                    • This works great. Thank you! The only change I had to make was getting rid of the preference_list.append (choices[choice]) as the number of preference list can be fewer than 3.

                      – sangurocactus
                      Nov 14 '18 at 6:13

















                    • This works great. Thank you! The only change I had to make was getting rid of the preference_list.append (choices[choice]) as the number of preference list can be fewer than 3.

                      – sangurocactus
                      Nov 14 '18 at 6:13
















                    This works great. Thank you! The only change I had to make was getting rid of the preference_list.append (choices[choice]) as the number of preference list can be fewer than 3.

                    – sangurocactus
                    Nov 14 '18 at 6:13





                    This works great. Thank you! The only change I had to make was getting rid of the preference_list.append (choices[choice]) as the number of preference list can be fewer than 3.

                    – sangurocactus
                    Nov 14 '18 at 6:13

















                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291213%2fcompiling-a-nested-list-with-conditional-statement%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







                    這個網誌中的熱門文章

                    Barbados

                    How to read a connectionString WITH PROVIDER in .NET Core?

                    Node.js Script on GitHub Pages or Amazon S3