Bottom Page

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'>
Quote
#2
filter object is generator. after first conversion it is exhausted (empty). That's why the second conversion yield empty tuple/list
Quote
#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
Quote
#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)
buran likes this post
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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to get first line of a tuple and the third item in its tuple. Need Help, Anybody? SukhmeetSingh 5 108 May-21-2019, 11:39 AM
Last Post: avorane
  py2 to py3 conversion Vysero 1 76 May-14-2019, 04:05 PM
Last Post: snippsat
  Error while fetching data from PostgreSQL tuple indices must be integers or slices, n Sandy777 6 122 May-12-2019, 11:41 AM
Last Post: Sandy777
  Evaluating multiple lists in a tuple cdogo 2 102 Apr-14-2019, 09:15 AM
Last Post: Yoriz
  httplib to http.client conversion issue zatlas1 1 89 Apr-12-2019, 05:21 PM
Last Post: Larz60+
  tuple index out of range delta1071 3 298 Apr-11-2019, 07:15 AM
Last Post: snippsat
  python opencv grayscale conversion error Spandora 0 161 Apr-03-2019, 05:45 PM
Last Post: Spandora
  Print out the tuple from the function main whatloop 2 117 Mar-25-2019, 10:27 AM
Last Post: whatloop
  Question about doc strings and tuple separator ? mcgrim 1 123 Mar-20-2019, 12:57 PM
Last Post: Yoriz
  TypeError: 'tuple' object is not callable marlinayakno 1 435 Mar-13-2019, 11:20 AM
Last Post: stullis

Forum Jump:


Users browsing this thread: 1 Guest(s)