Loop through two Lists
lst = [2,4,6]
lst2 = [4,8,9]
new_lst =
how do i go through every element in lst and multiply them by lst2[0], then go thought every element in lst and multiply them by lst2[1] and finally go through every element in lst and multiply them by lst2[2].
I will add all the answers new_lst
new_lst = [2*4, 4*4, 6*4, 2*8, 4*8, 6*8, 2*9, 4*9, 6*9]
loops
add a comment |
lst = [2,4,6]
lst2 = [4,8,9]
new_lst =
how do i go through every element in lst and multiply them by lst2[0], then go thought every element in lst and multiply them by lst2[1] and finally go through every element in lst and multiply them by lst2[2].
I will add all the answers new_lst
new_lst = [2*4, 4*4, 6*4, 2*8, 4*8, 6*8, 2*9, 4*9, 6*9]
loops
Are you writing in LOOP or what language do you use?
– Timothy Lukas H.
Nov 25 '18 at 13:04
I am using Python.
– Phil
Nov 25 '18 at 14:18
add a comment |
lst = [2,4,6]
lst2 = [4,8,9]
new_lst =
how do i go through every element in lst and multiply them by lst2[0], then go thought every element in lst and multiply them by lst2[1] and finally go through every element in lst and multiply them by lst2[2].
I will add all the answers new_lst
new_lst = [2*4, 4*4, 6*4, 2*8, 4*8, 6*8, 2*9, 4*9, 6*9]
loops
lst = [2,4,6]
lst2 = [4,8,9]
new_lst =
how do i go through every element in lst and multiply them by lst2[0], then go thought every element in lst and multiply them by lst2[1] and finally go through every element in lst and multiply them by lst2[2].
I will add all the answers new_lst
new_lst = [2*4, 4*4, 6*4, 2*8, 4*8, 6*8, 2*9, 4*9, 6*9]
loops
loops
asked Nov 25 '18 at 12:58
PhilPhil
113
113
Are you writing in LOOP or what language do you use?
– Timothy Lukas H.
Nov 25 '18 at 13:04
I am using Python.
– Phil
Nov 25 '18 at 14:18
add a comment |
Are you writing in LOOP or what language do you use?
– Timothy Lukas H.
Nov 25 '18 at 13:04
I am using Python.
– Phil
Nov 25 '18 at 14:18
Are you writing in LOOP or what language do you use?
– Timothy Lukas H.
Nov 25 '18 at 13:04
Are you writing in LOOP or what language do you use?
– Timothy Lukas H.
Nov 25 '18 at 13:04
I am using Python.
– Phil
Nov 25 '18 at 14:18
I am using Python.
– Phil
Nov 25 '18 at 14:18
add a comment |
2 Answers
2
active
oldest
votes
This may work,
lst = [2,4,6]
lst2 = [4,8,9]
new_lst =
for i in lst2:
# i will equal 4, then 8, then 9
for j in lst:
# j will equal 2, then 4, then 6
new_lst.append(i*j)
print(new_lst)
> [8, 16, 24, 16, 32, 48, 18, 36, 54]
1
Thanks very much, that worked!
– Phil
Nov 25 '18 at 18:30
add a comment |
I'm not based in Python, but in C# but because you didn't get any answer yet, I answer so you can try to rebuild:
In C# you would do:
List list = new List();
for(int i = 0; i < lst.length; i++)
{
for(int j = 0; j < lst2.length; j++)
{
list.Add(lst[i] * lst2[j]);
}
}
1
Thank you for your advise!
– Phil
Nov 25 '18 at 18: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%2f53467672%2floop-through-two-lists%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
This may work,
lst = [2,4,6]
lst2 = [4,8,9]
new_lst =
for i in lst2:
# i will equal 4, then 8, then 9
for j in lst:
# j will equal 2, then 4, then 6
new_lst.append(i*j)
print(new_lst)
> [8, 16, 24, 16, 32, 48, 18, 36, 54]
1
Thanks very much, that worked!
– Phil
Nov 25 '18 at 18:30
add a comment |
This may work,
lst = [2,4,6]
lst2 = [4,8,9]
new_lst =
for i in lst2:
# i will equal 4, then 8, then 9
for j in lst:
# j will equal 2, then 4, then 6
new_lst.append(i*j)
print(new_lst)
> [8, 16, 24, 16, 32, 48, 18, 36, 54]
1
Thanks very much, that worked!
– Phil
Nov 25 '18 at 18:30
add a comment |
This may work,
lst = [2,4,6]
lst2 = [4,8,9]
new_lst =
for i in lst2:
# i will equal 4, then 8, then 9
for j in lst:
# j will equal 2, then 4, then 6
new_lst.append(i*j)
print(new_lst)
> [8, 16, 24, 16, 32, 48, 18, 36, 54]
This may work,
lst = [2,4,6]
lst2 = [4,8,9]
new_lst =
for i in lst2:
# i will equal 4, then 8, then 9
for j in lst:
# j will equal 2, then 4, then 6
new_lst.append(i*j)
print(new_lst)
> [8, 16, 24, 16, 32, 48, 18, 36, 54]
edited Nov 25 '18 at 16:16
Andrew F
1,0541412
1,0541412
answered Nov 25 '18 at 14:54
ChameeraChameera
628
628
1
Thanks very much, that worked!
– Phil
Nov 25 '18 at 18:30
add a comment |
1
Thanks very much, that worked!
– Phil
Nov 25 '18 at 18:30
1
1
Thanks very much, that worked!
– Phil
Nov 25 '18 at 18:30
Thanks very much, that worked!
– Phil
Nov 25 '18 at 18:30
add a comment |
I'm not based in Python, but in C# but because you didn't get any answer yet, I answer so you can try to rebuild:
In C# you would do:
List list = new List();
for(int i = 0; i < lst.length; i++)
{
for(int j = 0; j < lst2.length; j++)
{
list.Add(lst[i] * lst2[j]);
}
}
1
Thank you for your advise!
– Phil
Nov 25 '18 at 18:30
add a comment |
I'm not based in Python, but in C# but because you didn't get any answer yet, I answer so you can try to rebuild:
In C# you would do:
List list = new List();
for(int i = 0; i < lst.length; i++)
{
for(int j = 0; j < lst2.length; j++)
{
list.Add(lst[i] * lst2[j]);
}
}
1
Thank you for your advise!
– Phil
Nov 25 '18 at 18:30
add a comment |
I'm not based in Python, but in C# but because you didn't get any answer yet, I answer so you can try to rebuild:
In C# you would do:
List list = new List();
for(int i = 0; i < lst.length; i++)
{
for(int j = 0; j < lst2.length; j++)
{
list.Add(lst[i] * lst2[j]);
}
}
I'm not based in Python, but in C# but because you didn't get any answer yet, I answer so you can try to rebuild:
In C# you would do:
List list = new List();
for(int i = 0; i < lst.length; i++)
{
for(int j = 0; j < lst2.length; j++)
{
list.Add(lst[i] * lst2[j]);
}
}
answered Nov 25 '18 at 14:39
Timothy Lukas H.Timothy Lukas H.
286212
286212
1
Thank you for your advise!
– Phil
Nov 25 '18 at 18:30
add a comment |
1
Thank you for your advise!
– Phil
Nov 25 '18 at 18:30
1
1
Thank you for your advise!
– Phil
Nov 25 '18 at 18:30
Thank you for your advise!
– Phil
Nov 25 '18 at 18: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%2f53467672%2floop-through-two-lists%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
Are you writing in LOOP or what language do you use?
– Timothy Lukas H.
Nov 25 '18 at 13:04
I am using Python.
– Phil
Nov 25 '18 at 14:18