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

Life of Brian: Conjugate the verb, "to go" !
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Code syntax with underscore and assigning to a tuple santoshbwn 1 142 Jun-24-2019, 12:05 PM
Last Post: ThomasL
  Arrange list of tuple using loop batchenr 7 341 Jun-16-2019, 03:24 PM
Last Post: Abdullah
  Call a Function contained in a Tuple SamGer 7 349 Jun-14-2019, 05:00 PM
Last Post: ThomasL
  Question on Tuple SamGer 2 182 Jun-03-2019, 02:14 PM
Last Post: DeaD_EyE
  python opencv grayscale conversion error Spandora 1 588 May-26-2019, 10:43 AM
Last Post: heiner55
  Vba conversion to Python stranger14u 1 481 May-26-2019, 08:01 AM
Last Post: heiner55
  AttributeError: 'tuple' object has no attribute 'interface' Kim6071 1 620 May-26-2019, 07:54 AM
Last Post: heiner55
  How to get first line of a tuple and the third item in its tuple. Need Help, Anybody? SukhmeetSingh 5 239 May-21-2019, 11:39 AM
Last Post: avorane
  py2 to py3 conversion Vysero 1 159 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 367 May-12-2019, 11:41 AM
Last Post: Sandy777

Forum Jump:


Users browsing this thread: 1 Guest(s)