Python add() function of set in list comprehension

Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I have come across the below code to remove duplicates from a list:
seen = set(); print [i for i in list if i not in seen and not seen.add(i)]
I could not comprehend what exactly "and not seen.add(i)" this part of code is doing as help(set.add) gives below explanation:
add(...)
Add an element to a set.
This has no effect if the element is already present.
Looking forward to your help in understanding it
python
add a comment |Â
up vote
1
down vote
favorite
I have come across the below code to remove duplicates from a list:
seen = set(); print [i for i in list if i not in seen and not seen.add(i)]
I could not comprehend what exactly "and not seen.add(i)" this part of code is doing as help(set.add) gives below explanation:
add(...)
Add an element to a set.
This has no effect if the element is already present.
Looking forward to your help in understanding it
python
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have come across the below code to remove duplicates from a list:
seen = set(); print [i for i in list if i not in seen and not seen.add(i)]
I could not comprehend what exactly "and not seen.add(i)" this part of code is doing as help(set.add) gives below explanation:
add(...)
Add an element to a set.
This has no effect if the element is already present.
Looking forward to your help in understanding it
python
I have come across the below code to remove duplicates from a list:
seen = set(); print [i for i in list if i not in seen and not seen.add(i)]
I could not comprehend what exactly "and not seen.add(i)" this part of code is doing as help(set.add) gives below explanation:
add(...)
Add an element to a set.
This has no effect if the element is already present.
Looking forward to your help in understanding it
python
asked Nov 28 '17 at 6:27
Ibrahim Quraish
1419
1419
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
The list comprehension iterates over the values of the original/input list. We want a value to be added to the new/output list if and only if it has not already been seen, hence the conditional expression if i not in seen. When a new value is added to the new/output list, the seen set has to be updated, hence the seen.add(i) function call. However the set.add() method returns None, which evaluates to False. Therefore the not operator is added, so that not seen.add(i) always returns True.
1
Awesome! thank you very much. I also got an understanding that and operator also acts like conditional statement's true part short-circuit behavior
â Ibrahim Quraish
Nov 28 '17 at 7:04
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
The list comprehension iterates over the values of the original/input list. We want a value to be added to the new/output list if and only if it has not already been seen, hence the conditional expression if i not in seen. When a new value is added to the new/output list, the seen set has to be updated, hence the seen.add(i) function call. However the set.add() method returns None, which evaluates to False. Therefore the not operator is added, so that not seen.add(i) always returns True.
1
Awesome! thank you very much. I also got an understanding that and operator also acts like conditional statement's true part short-circuit behavior
â Ibrahim Quraish
Nov 28 '17 at 7:04
add a comment |Â
up vote
1
down vote
accepted
The list comprehension iterates over the values of the original/input list. We want a value to be added to the new/output list if and only if it has not already been seen, hence the conditional expression if i not in seen. When a new value is added to the new/output list, the seen set has to be updated, hence the seen.add(i) function call. However the set.add() method returns None, which evaluates to False. Therefore the not operator is added, so that not seen.add(i) always returns True.
1
Awesome! thank you very much. I also got an understanding that and operator also acts like conditional statement's true part short-circuit behavior
â Ibrahim Quraish
Nov 28 '17 at 7:04
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
The list comprehension iterates over the values of the original/input list. We want a value to be added to the new/output list if and only if it has not already been seen, hence the conditional expression if i not in seen. When a new value is added to the new/output list, the seen set has to be updated, hence the seen.add(i) function call. However the set.add() method returns None, which evaluates to False. Therefore the not operator is added, so that not seen.add(i) always returns True.
The list comprehension iterates over the values of the original/input list. We want a value to be added to the new/output list if and only if it has not already been seen, hence the conditional expression if i not in seen. When a new value is added to the new/output list, the seen set has to be updated, hence the seen.add(i) function call. However the set.add() method returns None, which evaluates to False. Therefore the not operator is added, so that not seen.add(i) always returns True.
answered Nov 28 '17 at 6:46
igal
4,830930
4,830930
1
Awesome! thank you very much. I also got an understanding that and operator also acts like conditional statement's true part short-circuit behavior
â Ibrahim Quraish
Nov 28 '17 at 7:04
add a comment |Â
1
Awesome! thank you very much. I also got an understanding that and operator also acts like conditional statement's true part short-circuit behavior
â Ibrahim Quraish
Nov 28 '17 at 7:04
1
1
Awesome! thank you very much. I also got an understanding that and operator also acts like conditional statement's true part short-circuit behavior
â Ibrahim Quraish
Nov 28 '17 at 7:04
Awesome! thank you very much. I also got an understanding that and operator also acts like conditional statement's true part short-circuit behavior
â Ibrahim Quraish
Nov 28 '17 at 7:04
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f407424%2fpython-add-function-of-set-in-list-comprehension%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password