1

Udemy course: Loop over the items of the passwords list and in each iteration print out the item if the item contains the strings 'ba' or 'ab' inside.

passwords = ['ccavfb', 'baaded', 'bbaa', 'aaeed', 'vbb', 'aadeba', 'aba', 'dee', 'dade', 'abc', 'aae', 'dded', 'abb', 'aaf', 'ffaec']

I know i could create the following for loop for this and it would work

for x in passwords:
    if 'ab' in x or 'ba' in x:
        print(x)

but I just learned about list comprehension so i tried making the following function to go over the loop instead.

def checker(passes):
    return (x for x in passes if 'ab' in x or 'ba' in x)

print(checker(passwords))

this doesn't work however and gives me this error : <generator object checker.. at 0x00000212414B4110> I have no clue what this means even after looking at my old friend google for help. \I dont understand why this function isn't working please help explain to me what I'm missing. I'm completely lost.

this is the expected outcome according to the answer hint

baaded
bbaa
aadeba
aba
abc
abb
  • 4
    You're creating a generator comprehension, not a list comprehension, when you use `(` `)` parentheses instead of `[` `]` square brackets. You aren't given an error, that's just a generator. – Samathingamajig Nov 26 '22 at 06:25
  • 1
    Take a look at [Generator expressions vs. list comprehensions](https://stackoverflow.com/q/47789/11542834) for more info – Dash Nov 26 '22 at 06:28
  • Does this answer your question? [list comprehension returning "generator object..."](https://stackoverflow.com/questions/50494638/list-comprehension-returning-generator-object) – Gino Mempin Nov 26 '22 at 06:33
  • @Samathingamajig ~ Everybody is quick to jump on the square bracket train (including me, at first), but that is the wrong answer. Look at the OP's expected output. The answer is to join the generator with '\n'. – OneMadGypsy Nov 26 '22 at 06:33
  • thank you GinoMempin , Dash & Samathingamajig for pointing out that i created a generator and not a list comprehension - I didn't know it was before and the links you provided did enlighten me a bit. As @OneMadGpysy said, the way to get the output i wanted was to join with '\n' even if it meant i wasn't actually using a list comprehension. – Michele Staffiere Nov 26 '22 at 06:46
  • @OneMadGypsy I intentionally was not giving a full answer, both because this was a comment and to guide the OP to the answer. I envisioned, after converting to a list comprehension, `print(checker(passwords), sep="\n")` because that allows the result of the function to have meaning outside of just printing (yeah, you *can* split on `\n` after receiving a string, but that's not always possible). – Samathingamajig Nov 26 '22 at 06:54
  • @Samathingamajig ~ all that sounds great, but the OP didn't say anything about special print arguments, or any other after-the-fact process. The question was dead simple ~ I've got "this" and I need it to output "that". Anything else is off-topic. As far as not always being able to `split()` goes I'm assuming you are referring to the possibility of '\r\n'. Well, that isn't going to be an issue considering we specifically joined the string on '\n'. Even if there was some other issue with `split` it doesn't matter. That's for the OP to deal with based on the needs they expressed in their post. – OneMadGypsy Nov 26 '22 at 16:29

1 Answers1

1

Even if that did work it wouldn't give you the results you desire. If you want to use that style you will have to join it.

def checker(passes):
    return '\n'.join(x for x in passes if 'ab' in x or 'ba' in x)
OneMadGypsy
  • 4,640
  • 3
  • 10
  • 26