Created
February 5, 2020 07:00
-
-
Save TheLittleNaruto/921fb093bb2b73a0d078ca4fa70c72cf to your computer and use it in GitHub Desktop.
Comprehension in python dict or list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
list_ = list(range(6)) | |
for idx, val in enumerate(list_): | |
print(idx, val) | |
if idx%2: #we think we wrote code that deletes every item at odd indices | |
del list_[idx] | |
print(list_) | |
#compare to | |
list_ = list(range(6)) | |
new_list = [val for idx, val in enumerate(list_) if not idx%2] | |
print(new_list) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment