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


Messages In This Thread
Filter to List and/or Tuple conversion - by vindo - May-17-2019, 07:14 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  using > < for tuple , list,... akbarza 3 401 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,854 Jul-22-2022, 10:29 PM
Last Post: Skaperen
  why is my list a tuple CompleteNewb 7 2,214 Mar-17-2022, 10:09 PM
Last Post: CompleteNewb
  in a list or tuple Skaperen 6 78,412 May-16-2021, 09:59 PM
Last Post: Skaperen
  Create SQLite columns from a list or tuple? snakes 6 8,523 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,256 Jan-30-2021, 07:11 AM
Last Post: alloydog
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 2,728 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,488 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