how can I run two function in for loop step by step?
fruits=["apple","banana","melon"]
for fruit in fruits:
print fruit #print1
print len(fruit) #print2
I know output of this script will be like below
apple
5
banana
6
melon
5
but I want to run this script like
#print1 > #print1 > #print1 > #print2 > #print2 > #print2
so the output will be like
apple
banana
melon
5
6
5
Is there any good way for this?
(point is that I want to use for function one time)
(Sorry for bad expanation)
python for-loop
add a comment |
fruits=["apple","banana","melon"]
for fruit in fruits:
print fruit #print1
print len(fruit) #print2
I know output of this script will be like below
apple
5
banana
6
melon
5
but I want to run this script like
#print1 > #print1 > #print1 > #print2 > #print2 > #print2
so the output will be like
apple
banana
melon
5
6
5
Is there any good way for this?
(point is that I want to use for function one time)
(Sorry for bad expanation)
python for-loop
add a comment |
fruits=["apple","banana","melon"]
for fruit in fruits:
print fruit #print1
print len(fruit) #print2
I know output of this script will be like below
apple
5
banana
6
melon
5
but I want to run this script like
#print1 > #print1 > #print1 > #print2 > #print2 > #print2
so the output will be like
apple
banana
melon
5
6
5
Is there any good way for this?
(point is that I want to use for function one time)
(Sorry for bad expanation)
python for-loop
fruits=["apple","banana","melon"]
for fruit in fruits:
print fruit #print1
print len(fruit) #print2
I know output of this script will be like below
apple
5
banana
6
melon
5
but I want to run this script like
#print1 > #print1 > #print1 > #print2 > #print2 > #print2
so the output will be like
apple
banana
melon
5
6
5
Is there any good way for this?
(point is that I want to use for function one time)
(Sorry for bad expanation)
python for-loop
python for-loop
edited Nov 13 '18 at 3:19
asked Nov 13 '18 at 3:11
tehoo
344
344
add a comment |
add a comment |
7 Answers
7
active
oldest
votes
The simplest way to do this is to iterate the loop twice:
fruits = ["apple", "banana", "melon"]
for _ in range(2):
for fruit in fruits:
print fruit
which gives:
apple
banana
melon
apple
banana
melon
This avoids duplicating the list object, avoids duplicating code, and scales easily.
Edit: to iterate over two different functions, separately, you can just iterate over the list twice:
fruits = ["apple", "banana", "melon"]
for fruit in fruits:
print fruit
for fruit in fruits:
print len(fruit)
For more complex situations you could also make a list of your functions and iterate through those:
def print_name(item):
print(string)
def print_name_length(item):
print(len(item))
fruits = ["apple", "banana", "melon"]
for func in (print_name, print_name_length):
for fruit in fruits:
func(fruit)
thanks ! but what if two functions in the loop are different? is there any other way?
– tehoo
Nov 13 '18 at 3:27
No problem. See the edit for how to call two different functions.
– 101
Nov 13 '18 at 3:37
Thank you so much !! I thought there must be a way using only one for loop but I think there is no way like that!!
– tehoo
Nov 13 '18 at 3:54
add a comment |
You can create a list of functions you want to call and loop over them first, and then loop over fruits:
fruits = ["apple", "banana", "melon"]
for func in [lambda x: x, len]:
for fruit in fruits:
print(func(fruit))
Output
apple
banana
melon
5
6
5
add a comment |
It looks like you could just loop through the same list twice to achieve the desired logging:
for i in range(2):
for fruit in fruits:
print(fruit)
add a comment |
You can use generator expressions:
fruits=["apple","banana","melon"]
def getfruits(fruits):
for f in fruits:
print(f)
yield str(len(f)) + 'n'
print(*list(getfruits(fruits)))
Output:
apple
banana
melon
5
6
5
add a comment |
fruits=["apple","banana","melon"]
for lists in (fruits, (len(fruit) for fruit in fruits)):
for each_list in lists:
print(each_list)
I make a loop iterating over two lists; fruits and a list containing the lengths of the fruit in fruits.
Then I print out each list.
This could probably be more memory efficient, but this makes the most sense to me. I was going for readability.
add a comment |
You can just run the same loop twice.
fruits = ["apple","banana","melon"]
for f in fruits:
print f
for f in fruits:
print f
Which will get you the result. It may be ugly but it gets the job done.
add a comment |
There is no way to do this in one loop. Is there any reason why this would not work:
for fruit in fruits:
print fruit #print1
for fruit in fruits:
print fruit #print2
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%2f53273233%2fhow-can-i-run-two-function-in-for-loop-step-by-step%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
The simplest way to do this is to iterate the loop twice:
fruits = ["apple", "banana", "melon"]
for _ in range(2):
for fruit in fruits:
print fruit
which gives:
apple
banana
melon
apple
banana
melon
This avoids duplicating the list object, avoids duplicating code, and scales easily.
Edit: to iterate over two different functions, separately, you can just iterate over the list twice:
fruits = ["apple", "banana", "melon"]
for fruit in fruits:
print fruit
for fruit in fruits:
print len(fruit)
For more complex situations you could also make a list of your functions and iterate through those:
def print_name(item):
print(string)
def print_name_length(item):
print(len(item))
fruits = ["apple", "banana", "melon"]
for func in (print_name, print_name_length):
for fruit in fruits:
func(fruit)
thanks ! but what if two functions in the loop are different? is there any other way?
– tehoo
Nov 13 '18 at 3:27
No problem. See the edit for how to call two different functions.
– 101
Nov 13 '18 at 3:37
Thank you so much !! I thought there must be a way using only one for loop but I think there is no way like that!!
– tehoo
Nov 13 '18 at 3:54
add a comment |
The simplest way to do this is to iterate the loop twice:
fruits = ["apple", "banana", "melon"]
for _ in range(2):
for fruit in fruits:
print fruit
which gives:
apple
banana
melon
apple
banana
melon
This avoids duplicating the list object, avoids duplicating code, and scales easily.
Edit: to iterate over two different functions, separately, you can just iterate over the list twice:
fruits = ["apple", "banana", "melon"]
for fruit in fruits:
print fruit
for fruit in fruits:
print len(fruit)
For more complex situations you could also make a list of your functions and iterate through those:
def print_name(item):
print(string)
def print_name_length(item):
print(len(item))
fruits = ["apple", "banana", "melon"]
for func in (print_name, print_name_length):
for fruit in fruits:
func(fruit)
thanks ! but what if two functions in the loop are different? is there any other way?
– tehoo
Nov 13 '18 at 3:27
No problem. See the edit for how to call two different functions.
– 101
Nov 13 '18 at 3:37
Thank you so much !! I thought there must be a way using only one for loop but I think there is no way like that!!
– tehoo
Nov 13 '18 at 3:54
add a comment |
The simplest way to do this is to iterate the loop twice:
fruits = ["apple", "banana", "melon"]
for _ in range(2):
for fruit in fruits:
print fruit
which gives:
apple
banana
melon
apple
banana
melon
This avoids duplicating the list object, avoids duplicating code, and scales easily.
Edit: to iterate over two different functions, separately, you can just iterate over the list twice:
fruits = ["apple", "banana", "melon"]
for fruit in fruits:
print fruit
for fruit in fruits:
print len(fruit)
For more complex situations you could also make a list of your functions and iterate through those:
def print_name(item):
print(string)
def print_name_length(item):
print(len(item))
fruits = ["apple", "banana", "melon"]
for func in (print_name, print_name_length):
for fruit in fruits:
func(fruit)
The simplest way to do this is to iterate the loop twice:
fruits = ["apple", "banana", "melon"]
for _ in range(2):
for fruit in fruits:
print fruit
which gives:
apple
banana
melon
apple
banana
melon
This avoids duplicating the list object, avoids duplicating code, and scales easily.
Edit: to iterate over two different functions, separately, you can just iterate over the list twice:
fruits = ["apple", "banana", "melon"]
for fruit in fruits:
print fruit
for fruit in fruits:
print len(fruit)
For more complex situations you could also make a list of your functions and iterate through those:
def print_name(item):
print(string)
def print_name_length(item):
print(len(item))
fruits = ["apple", "banana", "melon"]
for func in (print_name, print_name_length):
for fruit in fruits:
func(fruit)
edited Nov 14 '18 at 23:48
answered Nov 13 '18 at 3:16
101
5,26422146
5,26422146
thanks ! but what if two functions in the loop are different? is there any other way?
– tehoo
Nov 13 '18 at 3:27
No problem. See the edit for how to call two different functions.
– 101
Nov 13 '18 at 3:37
Thank you so much !! I thought there must be a way using only one for loop but I think there is no way like that!!
– tehoo
Nov 13 '18 at 3:54
add a comment |
thanks ! but what if two functions in the loop are different? is there any other way?
– tehoo
Nov 13 '18 at 3:27
No problem. See the edit for how to call two different functions.
– 101
Nov 13 '18 at 3:37
Thank you so much !! I thought there must be a way using only one for loop but I think there is no way like that!!
– tehoo
Nov 13 '18 at 3:54
thanks ! but what if two functions in the loop are different? is there any other way?
– tehoo
Nov 13 '18 at 3:27
thanks ! but what if two functions in the loop are different? is there any other way?
– tehoo
Nov 13 '18 at 3:27
No problem. See the edit for how to call two different functions.
– 101
Nov 13 '18 at 3:37
No problem. See the edit for how to call two different functions.
– 101
Nov 13 '18 at 3:37
Thank you so much !! I thought there must be a way using only one for loop but I think there is no way like that!!
– tehoo
Nov 13 '18 at 3:54
Thank you so much !! I thought there must be a way using only one for loop but I think there is no way like that!!
– tehoo
Nov 13 '18 at 3:54
add a comment |
You can create a list of functions you want to call and loop over them first, and then loop over fruits:
fruits = ["apple", "banana", "melon"]
for func in [lambda x: x, len]:
for fruit in fruits:
print(func(fruit))
Output
apple
banana
melon
5
6
5
add a comment |
You can create a list of functions you want to call and loop over them first, and then loop over fruits:
fruits = ["apple", "banana", "melon"]
for func in [lambda x: x, len]:
for fruit in fruits:
print(func(fruit))
Output
apple
banana
melon
5
6
5
add a comment |
You can create a list of functions you want to call and loop over them first, and then loop over fruits:
fruits = ["apple", "banana", "melon"]
for func in [lambda x: x, len]:
for fruit in fruits:
print(func(fruit))
Output
apple
banana
melon
5
6
5
You can create a list of functions you want to call and loop over them first, and then loop over fruits:
fruits = ["apple", "banana", "melon"]
for func in [lambda x: x, len]:
for fruit in fruits:
print(func(fruit))
Output
apple
banana
melon
5
6
5
edited Nov 13 '18 at 3:30
answered Nov 13 '18 at 3:15
slider
8,07011129
8,07011129
add a comment |
add a comment |
It looks like you could just loop through the same list twice to achieve the desired logging:
for i in range(2):
for fruit in fruits:
print(fruit)
add a comment |
It looks like you could just loop through the same list twice to achieve the desired logging:
for i in range(2):
for fruit in fruits:
print(fruit)
add a comment |
It looks like you could just loop through the same list twice to achieve the desired logging:
for i in range(2):
for fruit in fruits:
print(fruit)
It looks like you could just loop through the same list twice to achieve the desired logging:
for i in range(2):
for fruit in fruits:
print(fruit)
answered Nov 13 '18 at 3:16
Riley Steele Parsons
23116
23116
add a comment |
add a comment |
You can use generator expressions:
fruits=["apple","banana","melon"]
def getfruits(fruits):
for f in fruits:
print(f)
yield str(len(f)) + 'n'
print(*list(getfruits(fruits)))
Output:
apple
banana
melon
5
6
5
add a comment |
You can use generator expressions:
fruits=["apple","banana","melon"]
def getfruits(fruits):
for f in fruits:
print(f)
yield str(len(f)) + 'n'
print(*list(getfruits(fruits)))
Output:
apple
banana
melon
5
6
5
add a comment |
You can use generator expressions:
fruits=["apple","banana","melon"]
def getfruits(fruits):
for f in fruits:
print(f)
yield str(len(f)) + 'n'
print(*list(getfruits(fruits)))
Output:
apple
banana
melon
5
6
5
You can use generator expressions:
fruits=["apple","banana","melon"]
def getfruits(fruits):
for f in fruits:
print(f)
yield str(len(f)) + 'n'
print(*list(getfruits(fruits)))
Output:
apple
banana
melon
5
6
5
answered Nov 13 '18 at 3:56
Rishabh Mishra
370210
370210
add a comment |
add a comment |
fruits=["apple","banana","melon"]
for lists in (fruits, (len(fruit) for fruit in fruits)):
for each_list in lists:
print(each_list)
I make a loop iterating over two lists; fruits and a list containing the lengths of the fruit in fruits.
Then I print out each list.
This could probably be more memory efficient, but this makes the most sense to me. I was going for readability.
add a comment |
fruits=["apple","banana","melon"]
for lists in (fruits, (len(fruit) for fruit in fruits)):
for each_list in lists:
print(each_list)
I make a loop iterating over two lists; fruits and a list containing the lengths of the fruit in fruits.
Then I print out each list.
This could probably be more memory efficient, but this makes the most sense to me. I was going for readability.
add a comment |
fruits=["apple","banana","melon"]
for lists in (fruits, (len(fruit) for fruit in fruits)):
for each_list in lists:
print(each_list)
I make a loop iterating over two lists; fruits and a list containing the lengths of the fruit in fruits.
Then I print out each list.
This could probably be more memory efficient, but this makes the most sense to me. I was going for readability.
fruits=["apple","banana","melon"]
for lists in (fruits, (len(fruit) for fruit in fruits)):
for each_list in lists:
print(each_list)
I make a loop iterating over two lists; fruits and a list containing the lengths of the fruit in fruits.
Then I print out each list.
This could probably be more memory efficient, but this makes the most sense to me. I was going for readability.
answered Nov 13 '18 at 4:25
Alex White
1738
1738
add a comment |
add a comment |
You can just run the same loop twice.
fruits = ["apple","banana","melon"]
for f in fruits:
print f
for f in fruits:
print f
Which will get you the result. It may be ugly but it gets the job done.
add a comment |
You can just run the same loop twice.
fruits = ["apple","banana","melon"]
for f in fruits:
print f
for f in fruits:
print f
Which will get you the result. It may be ugly but it gets the job done.
add a comment |
You can just run the same loop twice.
fruits = ["apple","banana","melon"]
for f in fruits:
print f
for f in fruits:
print f
Which will get you the result. It may be ugly but it gets the job done.
You can just run the same loop twice.
fruits = ["apple","banana","melon"]
for f in fruits:
print f
for f in fruits:
print f
Which will get you the result. It may be ugly but it gets the job done.
answered Nov 13 '18 at 3:15
David White
3901214
3901214
add a comment |
add a comment |
There is no way to do this in one loop. Is there any reason why this would not work:
for fruit in fruits:
print fruit #print1
for fruit in fruits:
print fruit #print2
add a comment |
There is no way to do this in one loop. Is there any reason why this would not work:
for fruit in fruits:
print fruit #print1
for fruit in fruits:
print fruit #print2
add a comment |
There is no way to do this in one loop. Is there any reason why this would not work:
for fruit in fruits:
print fruit #print1
for fruit in fruits:
print fruit #print2
There is no way to do this in one loop. Is there any reason why this would not work:
for fruit in fruits:
print fruit #print1
for fruit in fruits:
print fruit #print2
answered Nov 13 '18 at 3:15
The Zach Man
1146
1146
add a comment |
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.
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.
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%2f53273233%2fhow-can-i-run-two-function-in-for-loop-step-by-step%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