-5

I have a loop as

y = [1,2,3,4,5,6,7]

for x in y:
    if x == 3:
       return(x)
    elif x == 4:
       return(x)
    elif x = 6:
       return(x)
    else:
       return('not found')

I don't wanna print data, I just want to return the data and if the data gets returned, is there a way to handle all of such returned data at once a list?

Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
  • 2
    The simplest way is to create an empty list `result = []`, and then `result.append(x)` instead of `return x`, and then after the whole loop, `return result`. – abarnert Mar 12 '18 at 03:59
  • None of your code is wrapped in a function, so returning doesn't make much sense here. In fact, your code won't run, because you return outside a function. – user3483203 Mar 12 '18 at 03:59
  • 1
    The most idiomatic way is to `yield x` instead. That doesn't give you a list, but it gives you, almost like magic, a different kind of iterable that can be used like a list in most ways. Don't use this until you're ready to learn about generators and iterators—but once you are, you'll start using it all over the place. – abarnert Mar 12 '18 at 04:00
  • 1
    I would highly recommend checking out [What is the purpose of the return statement](https://stackoverflow.com/questions/7129285/what-is-the-purpose-of-the-return-statement), because this is not a proper use of it. – user3483203 Mar 12 '18 at 04:05
  • 1
    May be you are looking for something like `result = [x if x in {3,4,6} else 'not found' for x in y ] ` – niraj Mar 12 '18 at 04:13
  • @infiniti ayush, read my answer. You have horrible syntax errors. and you need to fix them. –  Mar 12 '18 at 04:16

4 Answers4

0

First of all, you have a syntax error: It should be elif x == 6: as = is the assignment operator and == is the comparison operator, and you are trying to compare, not assign.

Next, you need to put the for loop inside a function in order to return a value. return can only be used inside a function. Otherwise, it returns a syntax error. So, make a function and insert the for loop inside it like this:

def function (y):
    for x in y:
        if x in {3,4,6}:
           return (x)
        else:
           return ('not found')

Then, you need to call the function on y. Like this:

function(y)

It is unclear what you mean by "handle all of such returned data at once as list". Anyway, if you mean that you want to store all the returned data in a list, then create an empty list and append each return value of function to the empty list. Like this:

def function (y):
    emptyList = []
    for x in y:
        if x in {3,4,6}:
           emptyList.append(x)
        else:
           emptyList.append("not found")
    return emptyList

If you could provide a more clear question, I'll edit to fit your needs!

  • You'd almost certainly want to make `emptyList` a local variable, not a global that you keep appending to each time you call this function. – abarnert Mar 12 '18 at 04:08
  • You should just use `if x in {3,4,6}`, instead of having 3 ifs. – user3483203 Mar 12 '18 at 04:08
  • @abarnert i've edited to make emptyList a local variable –  Mar 12 '18 at 04:11
  • @BOi use a set not a list. Average lookup `O(1)` vs `O(n)` – user3483203 Mar 12 '18 at 04:11
  • First function wouldn't work. The first value `1` is not found and the function returns right away. – JahKnows Mar 12 '18 at 04:38
  • 1
    @JahKnows that's what the OP's for loop does too.. It reads the first vaue 1 and returns "not found". the OP simply asked how to return the data instead of printing and how he can handle the return values in a list. This is why I placed his for loop into a function. He didn't ask if the function logically works or not. And i don't know the objective of his program, so I didn't try to mess with it. –  Mar 12 '18 at 04:46
0

If I am understanding correctly, you have a list of numbers and want to return certain values to a separate list [3, 4, 6] should be in a separate list. I am assuming that you do not want to delete those values from the original list (with the remove method) but want to keep them in the original list and also add them to another list. Below is an example:

y = [1, 2, 3, 4, 5, 6, 7]

z = []

for x in y:
     if x == 3:
          z.append(x)

     if x == 4:
          z.append(x)

     if x == 6:
          z.append(x)

1. We create the y list that stores a series of numbers. 2. We create an empty list that will store the values that you want to store in a separate list. 3. We start the for loop. x Will be the variable for each individual number in the list as we iterate over each value in the list and y is your list. 4. Instead of returning if the value == 3, we can append to the empty list in order to store that particular value in a list. We will run the if statement for each value that you want in a separate list, and we will append that value to the new list. 5. The result is that you get z = [3, 4, 6], a new list with the values from the original list that you wanted to store in a new list.

Simeon Ikudabo
  • 2,152
  • 1
  • 10
  • 27
0

Below code will provide the solution for above question ...

Generic solution

def createGenerator(new_list, comp_list):
   for i in new_list:
       if i in comp_list:
           yield i

comp_list = [4,5,6]
new_list = range(10)  # Written for generic (can pass any list here )
mygenerator = createGenerator(new_list, comp_list)
for i in mygenerator:
    print i
4
5
6

Solution for above one:

def createGenerator(y):
   for i in y:
       if i in [4,5,6]:
           yield i
y = range(10)
mygenerator = createGenerator(y)
for i in mygenerator:
    print i
output:
4
5
6
Community
  • 1
  • 1
-2
y = [1,2,3,4,5,6,7]

result = []
for x in y:
    if x == 3:
        result.append(x)
    elif x == 4:
        result.append(x)
    elif x == 6:
        result.append(x)
    else:
        # or pass if you want to ignore
        result.append(None)
return result
scriptboy
  • 777
  • 8
  • 25