Compare two lists and replace values in list one with values in list 2












2















I have two lists.



a = [1,2,3,4,0,4,5,6,3,6,0,5,6,8,0,3]
b = [1,2, None,4,5,4,5,6,3,6,7,5,6,8,4, None]


I want a resulting list like this.



new_list = [1,2,3,4,5,4,5,6,3,6,7,5,6,8,4,3]


List b is only replacing the 0's in list a and not touching the other elements for example the None is not being replaced.



How can I get about doing this?
Thanks in advance. Please do let me know if you need any other information.



I have tried the following and it does not work.



_ = dict(zip(a,b))
for k,v in _.items():
if k == 0:
a = a.replace(k,v)









share|improve this question




















  • 2





    What have you tried so far?

    – Ian Quah
    Nov 23 '18 at 0:17











  • - = dict(zip(a,b)) for k,v in _.items(): if k == 0: a = a.replace(k,v)

    – Matt
    Nov 23 '18 at 0:21













  • For a = [0, 1, 0] and b = [None, 2, 3], what do you expect the result to be?

    – lifebalance
    Nov 23 '18 at 0:58
















2















I have two lists.



a = [1,2,3,4,0,4,5,6,3,6,0,5,6,8,0,3]
b = [1,2, None,4,5,4,5,6,3,6,7,5,6,8,4, None]


I want a resulting list like this.



new_list = [1,2,3,4,5,4,5,6,3,6,7,5,6,8,4,3]


List b is only replacing the 0's in list a and not touching the other elements for example the None is not being replaced.



How can I get about doing this?
Thanks in advance. Please do let me know if you need any other information.



I have tried the following and it does not work.



_ = dict(zip(a,b))
for k,v in _.items():
if k == 0:
a = a.replace(k,v)









share|improve this question




















  • 2





    What have you tried so far?

    – Ian Quah
    Nov 23 '18 at 0:17











  • - = dict(zip(a,b)) for k,v in _.items(): if k == 0: a = a.replace(k,v)

    – Matt
    Nov 23 '18 at 0:21













  • For a = [0, 1, 0] and b = [None, 2, 3], what do you expect the result to be?

    – lifebalance
    Nov 23 '18 at 0:58














2












2








2








I have two lists.



a = [1,2,3,4,0,4,5,6,3,6,0,5,6,8,0,3]
b = [1,2, None,4,5,4,5,6,3,6,7,5,6,8,4, None]


I want a resulting list like this.



new_list = [1,2,3,4,5,4,5,6,3,6,7,5,6,8,4,3]


List b is only replacing the 0's in list a and not touching the other elements for example the None is not being replaced.



How can I get about doing this?
Thanks in advance. Please do let me know if you need any other information.



I have tried the following and it does not work.



_ = dict(zip(a,b))
for k,v in _.items():
if k == 0:
a = a.replace(k,v)









share|improve this question
















I have two lists.



a = [1,2,3,4,0,4,5,6,3,6,0,5,6,8,0,3]
b = [1,2, None,4,5,4,5,6,3,6,7,5,6,8,4, None]


I want a resulting list like this.



new_list = [1,2,3,4,5,4,5,6,3,6,7,5,6,8,4,3]


List b is only replacing the 0's in list a and not touching the other elements for example the None is not being replaced.



How can I get about doing this?
Thanks in advance. Please do let me know if you need any other information.



I have tried the following and it does not work.



_ = dict(zip(a,b))
for k,v in _.items():
if k == 0:
a = a.replace(k,v)






python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 0:24







Matt

















asked Nov 23 '18 at 0:15









MattMatt

546




546








  • 2





    What have you tried so far?

    – Ian Quah
    Nov 23 '18 at 0:17











  • - = dict(zip(a,b)) for k,v in _.items(): if k == 0: a = a.replace(k,v)

    – Matt
    Nov 23 '18 at 0:21













  • For a = [0, 1, 0] and b = [None, 2, 3], what do you expect the result to be?

    – lifebalance
    Nov 23 '18 at 0:58














  • 2





    What have you tried so far?

    – Ian Quah
    Nov 23 '18 at 0:17











  • - = dict(zip(a,b)) for k,v in _.items(): if k == 0: a = a.replace(k,v)

    – Matt
    Nov 23 '18 at 0:21













  • For a = [0, 1, 0] and b = [None, 2, 3], what do you expect the result to be?

    – lifebalance
    Nov 23 '18 at 0:58








2




2





What have you tried so far?

– Ian Quah
Nov 23 '18 at 0:17





What have you tried so far?

– Ian Quah
Nov 23 '18 at 0:17













- = dict(zip(a,b)) for k,v in _.items(): if k == 0: a = a.replace(k,v)

– Matt
Nov 23 '18 at 0:21







- = dict(zip(a,b)) for k,v in _.items(): if k == 0: a = a.replace(k,v)

– Matt
Nov 23 '18 at 0:21















For a = [0, 1, 0] and b = [None, 2, 3], what do you expect the result to be?

– lifebalance
Nov 23 '18 at 0:58





For a = [0, 1, 0] and b = [None, 2, 3], what do you expect the result to be?

– lifebalance
Nov 23 '18 at 0:58












4 Answers
4






active

oldest

votes


















3














You can use zip and a simple list comprehension to generate a new list by picking elements of a if they're not 0 or elements of b if the corresponding a element is zero:



a = [1, 2, 3, 4, 0, 4, 5, 6, 3, 6, 0, 5, 6, 8, 0, 3]
b = [1, 2, None, 4, 5, 4, 5, 6, 3, 6, 7, 5, 6, 8, 4, None]

result = [y if x == 0 else x for x, y in zip(a, b)]
print(result)
# [1, 2, 3, 4, 5, 4, 5, 6, 3, 6, 7, 5, 6, 8, 4, 3]





share|improve this answer
























  • For a = [0, 1, 0] and b = [None, 2, 3], result will be [None, 1, 3]...is that what the OP wants?

    – lifebalance
    Nov 23 '18 at 1:04













  • I think so. If you don't want None, you can change the condition to y if x == 0 and y is not None ....

    – slider
    Nov 23 '18 at 1:16





















0














try with this:



a = [1, 0, 3, 4, 0, 6, 7, 0, 9]
b = [1, 2, None, 4, 5, 6, None, 8, 9]

k = 0 # Counter
c = [0] * len(a) # Creating the 'c' list
for n in a: # Reading 'a' list
if n != 0:
c[k] = a[k] # Copying 'a' list objects, when 'n' != 0, in 'c' list
else:
c[k] = b[k] # Copying 'b' list objects, when 'n' == 0, in 'c' list
k = k + 1





share|improve this answer































    0














    Or enumerate + a loop + indexing:



    l=[i for i,v in enumerate(a) if v==0]
    for i in l:
    a[i]=b[i]


    And now:



    print(a)


    Is:



    [1, 2, 3, 4, 5, 4, 5, 6, 3, 6, 7, 5, 6, 8, 4, 3]





    share|improve this answer
























    • You are effectively iterating over the length of the list twice to achieve the end result. That's inefficient.

      – lifebalance
      Nov 23 '18 at 1:32





















    0














    out = 
    for ea, eb in zip(a, b):
    res = ea
    # if element in a is 0 and corresponding element in b is not None
    if ea == 0 and eb:
    res = eb
    out.append(res)

    assert out == [1,2,3,4,5,4,5,6,3,6,7,5,6,8,4,3]


    And for a = [0, 1, 0] and b = [None, 2, 3] this will generate



    out == [0, 1, 3]





    share|improve this answer


























    • Wheres your explanation?

      – U9-Forward
      Nov 23 '18 at 0:48











    • The code is self-explanatory, yet added a comment at the risk of stating the obvious

      – lifebalance
      Nov 23 '18 at 1:30











    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%2f53439317%2fcompare-two-lists-and-replace-values-in-list-one-with-values-in-list-2%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    You can use zip and a simple list comprehension to generate a new list by picking elements of a if they're not 0 or elements of b if the corresponding a element is zero:



    a = [1, 2, 3, 4, 0, 4, 5, 6, 3, 6, 0, 5, 6, 8, 0, 3]
    b = [1, 2, None, 4, 5, 4, 5, 6, 3, 6, 7, 5, 6, 8, 4, None]

    result = [y if x == 0 else x for x, y in zip(a, b)]
    print(result)
    # [1, 2, 3, 4, 5, 4, 5, 6, 3, 6, 7, 5, 6, 8, 4, 3]





    share|improve this answer
























    • For a = [0, 1, 0] and b = [None, 2, 3], result will be [None, 1, 3]...is that what the OP wants?

      – lifebalance
      Nov 23 '18 at 1:04













    • I think so. If you don't want None, you can change the condition to y if x == 0 and y is not None ....

      – slider
      Nov 23 '18 at 1:16


















    3














    You can use zip and a simple list comprehension to generate a new list by picking elements of a if they're not 0 or elements of b if the corresponding a element is zero:



    a = [1, 2, 3, 4, 0, 4, 5, 6, 3, 6, 0, 5, 6, 8, 0, 3]
    b = [1, 2, None, 4, 5, 4, 5, 6, 3, 6, 7, 5, 6, 8, 4, None]

    result = [y if x == 0 else x for x, y in zip(a, b)]
    print(result)
    # [1, 2, 3, 4, 5, 4, 5, 6, 3, 6, 7, 5, 6, 8, 4, 3]





    share|improve this answer
























    • For a = [0, 1, 0] and b = [None, 2, 3], result will be [None, 1, 3]...is that what the OP wants?

      – lifebalance
      Nov 23 '18 at 1:04













    • I think so. If you don't want None, you can change the condition to y if x == 0 and y is not None ....

      – slider
      Nov 23 '18 at 1:16
















    3












    3








    3







    You can use zip and a simple list comprehension to generate a new list by picking elements of a if they're not 0 or elements of b if the corresponding a element is zero:



    a = [1, 2, 3, 4, 0, 4, 5, 6, 3, 6, 0, 5, 6, 8, 0, 3]
    b = [1, 2, None, 4, 5, 4, 5, 6, 3, 6, 7, 5, 6, 8, 4, None]

    result = [y if x == 0 else x for x, y in zip(a, b)]
    print(result)
    # [1, 2, 3, 4, 5, 4, 5, 6, 3, 6, 7, 5, 6, 8, 4, 3]





    share|improve this answer













    You can use zip and a simple list comprehension to generate a new list by picking elements of a if they're not 0 or elements of b if the corresponding a element is zero:



    a = [1, 2, 3, 4, 0, 4, 5, 6, 3, 6, 0, 5, 6, 8, 0, 3]
    b = [1, 2, None, 4, 5, 4, 5, 6, 3, 6, 7, 5, 6, 8, 4, None]

    result = [y if x == 0 else x for x, y in zip(a, b)]
    print(result)
    # [1, 2, 3, 4, 5, 4, 5, 6, 3, 6, 7, 5, 6, 8, 4, 3]






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 23 '18 at 0:23









    sliderslider

    8,23811129




    8,23811129













    • For a = [0, 1, 0] and b = [None, 2, 3], result will be [None, 1, 3]...is that what the OP wants?

      – lifebalance
      Nov 23 '18 at 1:04













    • I think so. If you don't want None, you can change the condition to y if x == 0 and y is not None ....

      – slider
      Nov 23 '18 at 1:16





















    • For a = [0, 1, 0] and b = [None, 2, 3], result will be [None, 1, 3]...is that what the OP wants?

      – lifebalance
      Nov 23 '18 at 1:04













    • I think so. If you don't want None, you can change the condition to y if x == 0 and y is not None ....

      – slider
      Nov 23 '18 at 1:16



















    For a = [0, 1, 0] and b = [None, 2, 3], result will be [None, 1, 3]...is that what the OP wants?

    – lifebalance
    Nov 23 '18 at 1:04







    For a = [0, 1, 0] and b = [None, 2, 3], result will be [None, 1, 3]...is that what the OP wants?

    – lifebalance
    Nov 23 '18 at 1:04















    I think so. If you don't want None, you can change the condition to y if x == 0 and y is not None ....

    – slider
    Nov 23 '18 at 1:16







    I think so. If you don't want None, you can change the condition to y if x == 0 and y is not None ....

    – slider
    Nov 23 '18 at 1:16















    0














    try with this:



    a = [1, 0, 3, 4, 0, 6, 7, 0, 9]
    b = [1, 2, None, 4, 5, 6, None, 8, 9]

    k = 0 # Counter
    c = [0] * len(a) # Creating the 'c' list
    for n in a: # Reading 'a' list
    if n != 0:
    c[k] = a[k] # Copying 'a' list objects, when 'n' != 0, in 'c' list
    else:
    c[k] = b[k] # Copying 'b' list objects, when 'n' == 0, in 'c' list
    k = k + 1





    share|improve this answer




























      0














      try with this:



      a = [1, 0, 3, 4, 0, 6, 7, 0, 9]
      b = [1, 2, None, 4, 5, 6, None, 8, 9]

      k = 0 # Counter
      c = [0] * len(a) # Creating the 'c' list
      for n in a: # Reading 'a' list
      if n != 0:
      c[k] = a[k] # Copying 'a' list objects, when 'n' != 0, in 'c' list
      else:
      c[k] = b[k] # Copying 'b' list objects, when 'n' == 0, in 'c' list
      k = k + 1





      share|improve this answer


























        0












        0








        0







        try with this:



        a = [1, 0, 3, 4, 0, 6, 7, 0, 9]
        b = [1, 2, None, 4, 5, 6, None, 8, 9]

        k = 0 # Counter
        c = [0] * len(a) # Creating the 'c' list
        for n in a: # Reading 'a' list
        if n != 0:
        c[k] = a[k] # Copying 'a' list objects, when 'n' != 0, in 'c' list
        else:
        c[k] = b[k] # Copying 'b' list objects, when 'n' == 0, in 'c' list
        k = k + 1





        share|improve this answer













        try with this:



        a = [1, 0, 3, 4, 0, 6, 7, 0, 9]
        b = [1, 2, None, 4, 5, 6, None, 8, 9]

        k = 0 # Counter
        c = [0] * len(a) # Creating the 'c' list
        for n in a: # Reading 'a' list
        if n != 0:
        c[k] = a[k] # Copying 'a' list objects, when 'n' != 0, in 'c' list
        else:
        c[k] = b[k] # Copying 'b' list objects, when 'n' == 0, in 'c' list
        k = k + 1






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 0:41







        user10693394






























            0














            Or enumerate + a loop + indexing:



            l=[i for i,v in enumerate(a) if v==0]
            for i in l:
            a[i]=b[i]


            And now:



            print(a)


            Is:



            [1, 2, 3, 4, 5, 4, 5, 6, 3, 6, 7, 5, 6, 8, 4, 3]





            share|improve this answer
























            • You are effectively iterating over the length of the list twice to achieve the end result. That's inefficient.

              – lifebalance
              Nov 23 '18 at 1:32


















            0














            Or enumerate + a loop + indexing:



            l=[i for i,v in enumerate(a) if v==0]
            for i in l:
            a[i]=b[i]


            And now:



            print(a)


            Is:



            [1, 2, 3, 4, 5, 4, 5, 6, 3, 6, 7, 5, 6, 8, 4, 3]





            share|improve this answer
























            • You are effectively iterating over the length of the list twice to achieve the end result. That's inefficient.

              – lifebalance
              Nov 23 '18 at 1:32
















            0












            0








            0







            Or enumerate + a loop + indexing:



            l=[i for i,v in enumerate(a) if v==0]
            for i in l:
            a[i]=b[i]


            And now:



            print(a)


            Is:



            [1, 2, 3, 4, 5, 4, 5, 6, 3, 6, 7, 5, 6, 8, 4, 3]





            share|improve this answer













            Or enumerate + a loop + indexing:



            l=[i for i,v in enumerate(a) if v==0]
            for i in l:
            a[i]=b[i]


            And now:



            print(a)


            Is:



            [1, 2, 3, 4, 5, 4, 5, 6, 3, 6, 7, 5, 6, 8, 4, 3]






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 23 '18 at 0:47









            U9-ForwardU9-Forward

            14.5k21338




            14.5k21338













            • You are effectively iterating over the length of the list twice to achieve the end result. That's inefficient.

              – lifebalance
              Nov 23 '18 at 1:32





















            • You are effectively iterating over the length of the list twice to achieve the end result. That's inefficient.

              – lifebalance
              Nov 23 '18 at 1:32



















            You are effectively iterating over the length of the list twice to achieve the end result. That's inefficient.

            – lifebalance
            Nov 23 '18 at 1:32







            You are effectively iterating over the length of the list twice to achieve the end result. That's inefficient.

            – lifebalance
            Nov 23 '18 at 1:32













            0














            out = 
            for ea, eb in zip(a, b):
            res = ea
            # if element in a is 0 and corresponding element in b is not None
            if ea == 0 and eb:
            res = eb
            out.append(res)

            assert out == [1,2,3,4,5,4,5,6,3,6,7,5,6,8,4,3]


            And for a = [0, 1, 0] and b = [None, 2, 3] this will generate



            out == [0, 1, 3]





            share|improve this answer


























            • Wheres your explanation?

              – U9-Forward
              Nov 23 '18 at 0:48











            • The code is self-explanatory, yet added a comment at the risk of stating the obvious

              – lifebalance
              Nov 23 '18 at 1:30
















            0














            out = 
            for ea, eb in zip(a, b):
            res = ea
            # if element in a is 0 and corresponding element in b is not None
            if ea == 0 and eb:
            res = eb
            out.append(res)

            assert out == [1,2,3,4,5,4,5,6,3,6,7,5,6,8,4,3]


            And for a = [0, 1, 0] and b = [None, 2, 3] this will generate



            out == [0, 1, 3]





            share|improve this answer


























            • Wheres your explanation?

              – U9-Forward
              Nov 23 '18 at 0:48











            • The code is self-explanatory, yet added a comment at the risk of stating the obvious

              – lifebalance
              Nov 23 '18 at 1:30














            0












            0








            0







            out = 
            for ea, eb in zip(a, b):
            res = ea
            # if element in a is 0 and corresponding element in b is not None
            if ea == 0 and eb:
            res = eb
            out.append(res)

            assert out == [1,2,3,4,5,4,5,6,3,6,7,5,6,8,4,3]


            And for a = [0, 1, 0] and b = [None, 2, 3] this will generate



            out == [0, 1, 3]





            share|improve this answer















            out = 
            for ea, eb in zip(a, b):
            res = ea
            # if element in a is 0 and corresponding element in b is not None
            if ea == 0 and eb:
            res = eb
            out.append(res)

            assert out == [1,2,3,4,5,4,5,6,3,6,7,5,6,8,4,3]


            And for a = [0, 1, 0] and b = [None, 2, 3] this will generate



            out == [0, 1, 3]






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 23 '18 at 1:25

























            answered Nov 23 '18 at 0:30









            lifebalancelifebalance

            91921444




            91921444













            • Wheres your explanation?

              – U9-Forward
              Nov 23 '18 at 0:48











            • The code is self-explanatory, yet added a comment at the risk of stating the obvious

              – lifebalance
              Nov 23 '18 at 1:30



















            • Wheres your explanation?

              – U9-Forward
              Nov 23 '18 at 0:48











            • The code is self-explanatory, yet added a comment at the risk of stating the obvious

              – lifebalance
              Nov 23 '18 at 1:30

















            Wheres your explanation?

            – U9-Forward
            Nov 23 '18 at 0:48





            Wheres your explanation?

            – U9-Forward
            Nov 23 '18 at 0:48













            The code is self-explanatory, yet added a comment at the risk of stating the obvious

            – lifebalance
            Nov 23 '18 at 1:30





            The code is self-explanatory, yet added a comment at the risk of stating the obvious

            – lifebalance
            Nov 23 '18 at 1:30


















            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%2f53439317%2fcompare-two-lists-and-replace-values-in-list-one-with-values-in-list-2%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

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            Refactoring coordinates for Minecraft Pi buildings written in Python