Python Forum
Filter to List and/or Tuple conversion
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Filter to List and/or Tuple conversion
#1
I am learning python and observed a curious thing while practicing.
I was playing around with filter and lambdas.

Question:
1. In the below script script1 i have converted the filter object into list and then tried to convert to tuple. What i observed was that the tuple resulted empty.
2. I thought may be filter does not like tuple :-).
I was curious if the sequence of conversion has something to do with this and changed the sequence. Now tuple first and then List script2.
Now, the list resulted empty

Can someone help me understand what is happening here and why?.


Script 1
print("Understanding FILTER")

filtr = lambda x:x**2
res=filter(filtr,[0,1,2,3,0,5,4,0])
print("Filter Result Type:: ",type(res))
print("Filter          ID:: ",id(res))
print("Filter Result (Converted to LIST ):: ",list(res))
print("Filter ID     (Converted to LIST ):: ",id(res))
print("Filter Result (Converted to TUPLE):: ",tuple(res))
print("Filter ID     (Converted to TUPLE):: ",id(res))
print("Filter Result Type:: ",type(res))
Result 1
Understanding FILTER
Filter Result Type::  <class 'filter'>
Filter          ID::  178889392
Filter Result (Converted to LIST )::  [1, 2, 3, 5, 4]
Filter ID     (Converted to LIST )::  178889392
Filter Result (Converted to TUPLE)::  ()
Filter ID     (Converted to TUPLE)::  178889392
Filter Result Type::  <class 'filter'>
Script 2
print("Understanding FILTER")

filtr = lambda x:x**2
res=filter(filtr,[0,1,2,3,0,5,4,0])
print("Filter Result Type:: ",type(res))
print("Filter          ID:: ",id(res))
print("Filter Result (Converted to TUPLE):: ",tuple(res))
print("Filter ID     (Converted to TUPLE):: ",id(res))
print("Filter Result (Converted to LIST ):: ",list(res))
print("Filter ID     (Converted to LIST ):: ",id(res))
print("Filter Result Type:: ",type(res))
Result 2
Understanding FILTER
Filter Result Type::  <class 'filter'>
Filter          ID::  178890176
Filter Result (Converted to TUPLE)::  (1, 2, 3, 5, 4)
Filter ID     (Converted to TUPLE)::  178890176
Filter Result (Converted to LIST )::  []
Filter ID     (Converted to LIST )::  178890176
Filter Result Type::  <class 'filter'>
Reply
#2
filter object is generator. after first conversion it is exhausted (empty). That's why the second conversion yield empty tuple/list
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(May-17-2019, 07:20 PM)buran Wrote: filter object is generator. after first conversion it is exhausted (empty). That's why the second conversion yield empty tuple/list

Excellent! and Thank you
This makes sense and a new concept for me to explore.
Appreciate the quick response.

Script
print("Understanding FILTER")

filtr = lambda x:x**2
res=filter(filtr,[0,1,2,3,0,5,4,0])
print("Filter Result Type:: ",type(res))
print("Filter          ID:: ",id(res))
print("Filter Result (Converted to TUPLE):: ",tuple(res))
print("Filter ID     (Converted to TUPLE):: ",id(res))
print("Filter Result (Converted to TUPLE):: ",tuple(res))
print("Filter ID     (Converted to TUPLE):: ",id(res))
Result
Understanding FILTER
Filter Result Type::  <class 'filter'>
Filter          ID::  178889000
Filter Result (Converted to TUPLE)::  (1, 2, 3, 5, 4)
Filter ID     (Converted to TUPLE)::  178889000
Filter Result (Converted to TUPLE)::  ()
Filter ID     (Converted to TUPLE)::  178889000
Reply
#4
Gentle reminder from PEP8 -> Programming Recommendations

Quote:Always use a def statement instead of an assignment statement that binds a lambda expression directly to an identifier.

Yes:

def f(x): return 2*x

No:

f = lambda x: 2*x

The first form means that the name of the resulting function object is specifically 'f' instead of the generic '<lambda>'. This is more useful for tracebacks and string representations in general. The use of the assignment statement eliminates the sole benefit a lambda expression can offer over an explicit def statement (i.e. that it can be embedded inside a larger expression)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  using > < for tuple , list,... akbarza 3 400 Feb-05-2024, 01:18 PM
Last Post: deanhystad
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 427 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Change font in a list or tuple apffal 4 2,635 Jun-16-2023, 02:55 AM
Last Post: schriftartenio
  search a list or tuple for a specific type ot class Skaperen 8 1,853 Jul-22-2022, 10:29 PM
Last Post: Skaperen
  why is my list a tuple CompleteNewb 7 2,213 Mar-17-2022, 10:09 PM
Last Post: CompleteNewb
  in a list or tuple Skaperen 6 78,104 May-16-2021, 09:59 PM
Last Post: Skaperen
  Create SQLite columns from a list or tuple? snakes 6 8,516 May-04-2021, 12:06 PM
Last Post: snakes
  What type of *data* is the name of a list/tuple/dict, etc? alloydog 9 4,255 Jan-30-2021, 07:11 AM
Last Post: alloydog
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 2,726 Nov-04-2020, 11:26 AM
Last Post: Aggam
  Python Error- TypeError: ('Params must be in a list, tuple, or Row', 'HY000') DarkCoder2020 3 5,486 Jul-29-2020, 12:02 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020