-1

Is there anyway to make this into a single statement in python ?

name = 'Customer_Identify'
useless_list = ['id']
# I Want to make the 3 following lines into a single line. It should return True.
for i in useless_list:
    if (i in name.lower()):
        print(True)

I tried with lambda functions but I maybe doing some mistake somewhere. :(

JSVJ
  • 500
  • 3
  • 14

4 Answers4

4

Since the question asked that it should return true. how about this ?

return any(i in name.lower() for i in useless_list)

ruhaib
  • 604
  • 5
  • 12
  • 1
    Not sure that's the same functionality... Here you return/print one boolean value if any of the values are in name. In the OP, only `True` is printed **for each** value that is in name... – Tomerikoo Aug 19 '19 at 15:50
  • 1
    @Tomerikoo The comment in the OP's code states it wants to return the value, i just assume the print was used unintentionally perhaps ? – ruhaib Aug 19 '19 at 15:53
  • Oh didn't notice that comment and in that case I guess that satisfies the problem. It is indeed a little unclear... Actually, if that is really the intention I guess this is the best answer – Tomerikoo Aug 19 '19 at 15:54
3

How about using generator expressions?

print('\n'.join(str(True) for i in useless_list if i in name.lower()))
Óscar López
  • 232,561
  • 37
  • 312
  • 386
0
name = 'Customer_Identify'
useless_list = ['id', 'AA']

[print (item in name.lower()) for item in useless_list]

output:

True
False
ncica
  • 7,015
  • 1
  • 15
  • 37
0

That's pretty awful, but you can use a list comprehension to actually invoke print:

[print(True) for i in useless_list if i in name.lower()]

Of course, that builds a totally useless list of Nones...

Right leg
  • 16,080
  • 7
  • 48
  • 81