Skip to content

Instantly share code, notes, and snippets.

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 TheLittleNaruto/921fb093bb2b73a0d078ca4fa70c72cf to your computer and use it in GitHub Desktop.
Save TheLittleNaruto/921fb093bb2b73a0d078ca4fa70c72cf to your computer and use it in GitHub Desktop.
Comprehension in python dict or list
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