Skip to content

Instantly share code, notes, and snippets.

@dradecic
Created September 14, 2019 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dradecic/9f23eb0c8073ecc8957f8fd533388cef to your computer and use it in GitHub Desktop.
Save dradecic/9f23eb0c8073ecc8957f8fd533388cef to your computer and use it in GitHub Desktop.
art7_list_comprehensions
titles = [
'This is a text without question mark',
'I contain question mark at the end?',
'No question mark for me...',
'What? A question mark? Sure.'
]
results = []
for title in titles:
if '?' in title:
results.append(1)
else:
results.append(0)
print(results) # [0, 1, 0, 1]
# List Comprehensions
[1 if '?' in title else 0 for title in titles] # [0, 1, 0, 1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment