Compare two lists and replace values in list one with values in list 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)
python
add a comment |
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
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
Fora = [0, 1, 0]
andb = [None, 2, 3]
, what do you expect the result to be?
– lifebalance
Nov 23 '18 at 0:58
add a comment |
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
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
python
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
Fora = [0, 1, 0]
andb = [None, 2, 3]
, what do you expect the result to be?
– lifebalance
Nov 23 '18 at 0:58
add a comment |
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
Fora = [0, 1, 0]
andb = [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
add a comment |
4 Answers
4
active
oldest
votes
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]
Fora = [0, 1, 0]
andb = [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 wantNone
, you can change the condition toy if x == 0 and y is not None ...
.
– slider
Nov 23 '18 at 1:16
add a comment |
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
add a comment |
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]
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
add a comment |
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]
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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]
Fora = [0, 1, 0]
andb = [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 wantNone
, you can change the condition toy if x == 0 and y is not None ...
.
– slider
Nov 23 '18 at 1:16
add a comment |
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]
Fora = [0, 1, 0]
andb = [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 wantNone
, you can change the condition toy if x == 0 and y is not None ...
.
– slider
Nov 23 '18 at 1:16
add a comment |
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]
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]
answered Nov 23 '18 at 0:23
sliderslider
8,23811129
8,23811129
Fora = [0, 1, 0]
andb = [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 wantNone
, you can change the condition toy if x == 0 and y is not None ...
.
– slider
Nov 23 '18 at 1:16
add a comment |
Fora = [0, 1, 0]
andb = [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 wantNone
, you can change the condition toy 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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 23 '18 at 0:41
user10693394
add a comment |
add a comment |
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]
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
add a comment |
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]
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
add a comment |
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]
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]
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
add a comment |
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
add a comment |
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]
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
add a comment |
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]
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
add a comment |
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]
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]
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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]
andb = [None, 2, 3]
, what do you expect the result to be?– lifebalance
Nov 23 '18 at 0:58