Python Forum
Simple code question about lambda and tuples
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple code question about lambda and tuples
#1
I have a simple code:

my_tuple = (0,1,2,3,4,5,6)
foo = list(filter(lambda x: x-0, my_tuple))

print(foo)
I know what the output is and that it removes the first entry, the 0. I could also change it as follows:

my_tuple = (0,1,2,3,4,5,6)
foo = list(filter(lambda x: x-0 and x-1, my_tuple))

print(foo)
I thought that tuples are immutable? How are the 0 and 1 removed? How does this work and what is this x-0 actually doing?
Yoriz write Oct-03-2021, 02:22 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
foo is a list made by using my_tuple, if you print the tuple my_tuple you will find it has not changed.
JasPyt likes this post
Reply
#3
Remember that 0 is treated as false when it is tested for its truth value (see the docs), so x - 0 will evaluate to True for any value that isn't 0. It's a strange way to write that predicate - it would be easier to read written as x != 0, obviously.
JasPyt likes this post
Reply
#4
Or as x not in (0, 1)
JasPyt likes this post
Reply
#5
(Oct-03-2021, 02:35 PM)ndc85430 Wrote: Remember that 0 is treated as false when it is tested for its truth value (see the docs), so x - 0 will evaluate to True for any value that isn't 0. It's a strange way to write that predicate - it would be easier to read written as x != 0, obviously.

Yes exactly != is what I would have expected. But ok, thanks for the answers!

So the point is:
it starts with x=0 and does the following:
0-0 and 0-1 which is: False and True => gives False
then it continues with the next element in the tuple, the 1:
1-0 and 1-1 which is: True and False => gives False
then 2:
2-0 and 1-1 which is: True and True => gives True

and so on, so that is why 0 and 1 are filtered out. Right?
Reply
#6
(Oct-03-2021, 04:50 PM)JasPyt Wrote: 2-0 and 1-1 which is: True and True => gives True
Output:
>>> 2-0 and 1-1 0
0 is False

but I guess you mean

Output:
>>> 2-0 and 2-1 1
JasPyt likes this post
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
#7
(Oct-03-2021, 07:02 PM)buran Wrote:
(Oct-03-2021, 04:50 PM)JasPyt Wrote: 2-0 and 1-1 which is: True and True => gives True
Output:
>>> 2-0 and 1-1 0
0 is False

but I guess you mean

Output:
>>> 2-0 and 2-1 1

Yes exactly!
Reply
#8
The code is confusing if write like this will most will figure what's going with a quick look.
def number_remove(x: int) -> int:
    if x not in (0, 1):
        return x

my_tuple = (0,1,2,3,4,5,6)
print(list(filter(number_remove, my_tuple)))
Output:
[2, 3, 4, 5, 6]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with simple code JacobSkinner 1 226 Mar-18-2024, 08:08 PM
Last Post: deanhystad
  I have a code which is very simple but still I cannot detect what's wrong with it max22 1 439 Nov-07-2023, 04:32 PM
Last Post: snippsat
  Simple Question - ' defined as "a". ?' Ryan012 10 1,490 May-27-2023, 06:03 PM
Last Post: Ryan012
  Very simple question about filenames and backslashes! garynewport 4 1,830 Jan-17-2023, 05:02 AM
Last Post: deanhystad
  Python Tkinter Simple Multithreading Question AaronCatolico1 5 1,468 Dec-14-2022, 11:35 PM
Last Post: deanhystad
  help me simple code result min and max number abrahimusmaximus 2 869 Nov-12-2022, 07:52 AM
Last Post: buran
  A simple "If...Else" question from a beginner Serena2022 6 1,638 Jul-11-2022, 05:59 AM
Last Post: Serena2022
  Simple encoding code ebolisa 3 1,399 Jun-18-2022, 10:59 AM
Last Post: deanhystad
  How would you (as an python expert) make this code more efficient/simple coder_sw99 3 1,750 Feb-21-2022, 10:52 AM
Last Post: Gribouillis
  Simple arithmetic question ebolisa 5 2,007 Dec-15-2021, 04:56 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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