0

I am trying to solve a class assignment as listed below

Use lambda expressions and the filter() function to filter out words from a list that don't start with the letter 's'. For example:

seq = ['soup','dog','salad','cat','great']

should be filtered down to:

['soup','salad'] 

I wrote the following two sets of code...

seq = ['soup', 'dog','salad', 'cat', 'great']
result = filter (lambda test: test[0] == "s",seq)
print (result)

seq = ['soup', 'dog','salad', 'cat', 'great']
def checkf (input):
    if input[0]=='s':
            a = "TRUE"
    return(a)
test = seq[0]
result = filter (checkf(test),seq)
print (result)

Both codes give a strange answer filter object at 0x112aa06d8 as if it is returning a memory address in hexadecimal... What's wrong?

khelwood
  • 55,782
  • 14
  • 81
  • 108
hvg2017
  • 23
  • 1
  • 3
  • I have edited your question to more appropriately reflect a more readable question see https://stackoverflow.com/help/how-to-ask for further help in regards to asking – Tyler Cowan Jan 25 '18 at 23:55
  • 2
    Possible duplicate of [Why does foo = filter(...) return a , not a list?](https://stackoverflow.com/questions/33174276/why-does-foo-filter-return-a-filter-object-not-a-list) – cowbert Jan 25 '18 at 23:58
  • 1
    In Python 3, `filter` returns a special filter object rather than a list. You can convert it to a list using the `list` function. – khelwood Jan 25 '18 at 23:58
  • what is wrong with the first code. Works for me with python 2. – Monish K Nair Jan 26 '18 at 00:00
  • 2
    Possible duplicate of [How to use filter, map, and reduce in Python 3](https://stackoverflow.com/questions/13638898/how-to-use-filter-map-and-reduce-in-python-3) – melwil Jan 26 '18 at 00:00

3 Answers3

3

In python 3, filter objects are iterators. To print the contents, you have to materialize it into a sequence first, so try:

print( list(result))

See also: How to use filter, map, and reduce in Python 3

melwil
  • 2,547
  • 1
  • 19
  • 34
cowbert
  • 3,212
  • 2
  • 25
  • 34
0

To use lambda and filter to resolve this problem, you need to specify that the result of your code will be a list.

So the following will work:

seq = ['soup', 'dog','salad', 'cat', 'great']
list(filter (lambda test: test[0] == "s",seq))

or (assigning to an object):

seq = ['soup', 'dog','salad', 'cat', 'great']
result = list(filter (lambda test: test[0] == "s",seq))
print(result)

The result of the two codes will be:

['soup', 'salad']
Leda Grasiele
  • 413
  • 1
  • 6
  • 14
0

This is the below code where your seq can be solved according to the lambda function.

seq = ['soup', 'dog','salad', 'cat', 'great']
result = list(filter (lambda test: test[0] == "s",seq))
print(result)

output: ['soup', 'salad']

using it in a function

seq = ['soup', 'dog','salad', 'cat', 'great']
def check(input):
    result = list(filter (lambda test: test[0] == "s",seq))
    return result

check(seq)

output: ['soup', 'salad']

cnnr
  • 1,267
  • 4
  • 18
  • 23
Debi
  • 18
  • 6