Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a return statement in a for loop?

Tags:

So I am working on a chat-bot for discord, and right now on a feature that would work as a todo-list. I have a command to add tasks to the list, where they are stored in a dict. However my problem is returning the list in a more readable format (see pictures).

def show_todo():     for key, value in cal.items():         print(value[0], key) 

The tasks are stored in a dict called cal. But in order for the bot to actually send the message I need to use a return statement, otherwise it'll just print it to the console and not to the actual chat (see pictures).

def show_todo():     for key, value in cal.items():         return(value[0], key) 

Here is how I tried to fix it, but since I used return the for-loop does not work properly.

So how do I fix this? How can I use a return statement so that it would print into the chat instead of the console?

like image 710
Eric.L Avatar asked Jun 15 '17 09:06

Eric.L


People also ask

Can I use return statement in for loop?

The return statement is useful because it saves time and makes the program run faster by returning the output of method without executing unnecessary code and loops. It is good practice to always have a return statement after the for/while loop in case the return statement inside the for/while loop is never executed.

How do you return data from a for loop?

You cannot return a value from a for loop in java. Only methods have the privilege of returning values. for loop is a control flow statement whose purpose is to iterate through the number of times till the condition is true. While iterating, you can define statements which needs to be executed.

Can you use return in for loop Python?

Using a return inside of a loop will break it and exit the function even if the iteration is still not finished. In some cases we need to break the loop if some conditions are met. However, in your current code, breaking the loop before finishing it is unintentional.

Can I use return in for loop in C?

The answer is yes, it will return.


1 Answers

Using a return inside of a loop will break it and exit the function even if the iteration is still not finished.

For example:

def num():     # Here there will be only one iteration     # For number == 1 => 1 % 2 = 1     # So, break the loop and return the number     for number in range(1, 10):         if number % 2:             return number >>> num() 1 

In some cases we need to break the loop if some conditions are met. However, in your current code, breaking the loop before finishing it is unintentional.

Instead of that, you can use a different approach:

Yielding your data

def show_todo():     # Create a generator     for key, value in cal.items():         yield value[0], key 

You can call it like:

a = list(show_todo())  # or tuple(show_todo()) 

or you can iterate through it:

for v, k in show_todo(): ... 

Putting your data into a list or other container

Append your data to a list, then return it after the end of your loop:

def show_todo():     my_list = []     for key, value in cal.items():         my_list.append((value[0], key))     return my_list 

Or use a list comprehension:

def show_todo():     return [(value[0], key) for key, value in cal.items()] 
like image 84
Chiheb Nexus Avatar answered Sep 24 '22 01:09

Chiheb Nexus