If/else in python list comprehension











up vote
-1
down vote

favorite












I would like to return random word from file, based on passed argument. But if the argument doesn't match anythning I dont want to return anything. My method looks like:



def word_from_score(self,score):
print(random.choices([word for word in self.file if sum([LETTER_SCORES[letter] for letter in word ]) == score]))


It returns the correct word from file based on passed argument in command line, but if the argument doesnt match, i want to return nothing, like ''. How could I add else to this statement?










share|improve this question


















  • 2




    Pleae provide a Minimal, Complete, and Verifiable example with sample input and expected output.
    – timgeb
    Nov 10 at 22:48















up vote
-1
down vote

favorite












I would like to return random word from file, based on passed argument. But if the argument doesn't match anythning I dont want to return anything. My method looks like:



def word_from_score(self,score):
print(random.choices([word for word in self.file if sum([LETTER_SCORES[letter] for letter in word ]) == score]))


It returns the correct word from file based on passed argument in command line, but if the argument doesnt match, i want to return nothing, like ''. How could I add else to this statement?










share|improve this question


















  • 2




    Pleae provide a Minimal, Complete, and Verifiable example with sample input and expected output.
    – timgeb
    Nov 10 at 22:48













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I would like to return random word from file, based on passed argument. But if the argument doesn't match anythning I dont want to return anything. My method looks like:



def word_from_score(self,score):
print(random.choices([word for word in self.file if sum([LETTER_SCORES[letter] for letter in word ]) == score]))


It returns the correct word from file based on passed argument in command line, but if the argument doesnt match, i want to return nothing, like ''. How could I add else to this statement?










share|improve this question













I would like to return random word from file, based on passed argument. But if the argument doesn't match anythning I dont want to return anything. My method looks like:



def word_from_score(self,score):
print(random.choices([word for word in self.file if sum([LETTER_SCORES[letter] for letter in word ]) == score]))


It returns the correct word from file based on passed argument in command line, but if the argument doesnt match, i want to return nothing, like ''. How could I add else to this statement?







python list-comprehension






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 22:46









Frendom

266




266








  • 2




    Pleae provide a Minimal, Complete, and Verifiable example with sample input and expected output.
    – timgeb
    Nov 10 at 22:48














  • 2




    Pleae provide a Minimal, Complete, and Verifiable example with sample input and expected output.
    – timgeb
    Nov 10 at 22:48








2




2




Pleae provide a Minimal, Complete, and Verifiable example with sample input and expected output.
– timgeb
Nov 10 at 22:48




Pleae provide a Minimal, Complete, and Verifiable example with sample input and expected output.
– timgeb
Nov 10 at 22:48












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










Should be:



def word_from_score(self,score):
print(random.choices([(word if sum([LETTER_SCORES[letter] for letter in word ]) == score else "") for word in self.file]))


The (... if ... else ...) is actually the ternary operator and not part of the surrounding list comprehension.






share|improve this answer























  • EDIT: it doesnt work, atm it returns " even if in file is word which sum of letter is passed in argument
    – Frendom
    Nov 11 at 19:15










  • @Frendom (1) Have you checked that it works with your original function? (2) Can you give a simple example for testing?
    – Michael Butscher
    Nov 11 at 22:23











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244172%2fif-else-in-python-list-comprehension%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










Should be:



def word_from_score(self,score):
print(random.choices([(word if sum([LETTER_SCORES[letter] for letter in word ]) == score else "") for word in self.file]))


The (... if ... else ...) is actually the ternary operator and not part of the surrounding list comprehension.






share|improve this answer























  • EDIT: it doesnt work, atm it returns " even if in file is word which sum of letter is passed in argument
    – Frendom
    Nov 11 at 19:15










  • @Frendom (1) Have you checked that it works with your original function? (2) Can you give a simple example for testing?
    – Michael Butscher
    Nov 11 at 22:23















up vote
0
down vote



accepted










Should be:



def word_from_score(self,score):
print(random.choices([(word if sum([LETTER_SCORES[letter] for letter in word ]) == score else "") for word in self.file]))


The (... if ... else ...) is actually the ternary operator and not part of the surrounding list comprehension.






share|improve this answer























  • EDIT: it doesnt work, atm it returns " even if in file is word which sum of letter is passed in argument
    – Frendom
    Nov 11 at 19:15










  • @Frendom (1) Have you checked that it works with your original function? (2) Can you give a simple example for testing?
    – Michael Butscher
    Nov 11 at 22:23













up vote
0
down vote



accepted







up vote
0
down vote



accepted






Should be:



def word_from_score(self,score):
print(random.choices([(word if sum([LETTER_SCORES[letter] for letter in word ]) == score else "") for word in self.file]))


The (... if ... else ...) is actually the ternary operator and not part of the surrounding list comprehension.






share|improve this answer














Should be:



def word_from_score(self,score):
print(random.choices([(word if sum([LETTER_SCORES[letter] for letter in word ]) == score else "") for word in self.file]))


The (... if ... else ...) is actually the ternary operator and not part of the surrounding list comprehension.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 10 at 22:55

























answered Nov 10 at 22:50









Michael Butscher

3,93811320




3,93811320












  • EDIT: it doesnt work, atm it returns " even if in file is word which sum of letter is passed in argument
    – Frendom
    Nov 11 at 19:15










  • @Frendom (1) Have you checked that it works with your original function? (2) Can you give a simple example for testing?
    – Michael Butscher
    Nov 11 at 22:23


















  • EDIT: it doesnt work, atm it returns " even if in file is word which sum of letter is passed in argument
    – Frendom
    Nov 11 at 19:15










  • @Frendom (1) Have you checked that it works with your original function? (2) Can you give a simple example for testing?
    – Michael Butscher
    Nov 11 at 22:23
















EDIT: it doesnt work, atm it returns " even if in file is word which sum of letter is passed in argument
– Frendom
Nov 11 at 19:15




EDIT: it doesnt work, atm it returns " even if in file is word which sum of letter is passed in argument
– Frendom
Nov 11 at 19:15












@Frendom (1) Have you checked that it works with your original function? (2) Can you give a simple example for testing?
– Michael Butscher
Nov 11 at 22:23




@Frendom (1) Have you checked that it works with your original function? (2) Can you give a simple example for testing?
– Michael Butscher
Nov 11 at 22:23


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244172%2fif-else-in-python-list-comprehension%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation