Skip to content

Instantly share code, notes, and snippets.

@immkavin-ranks
Last active December 13, 2023 11:23
Show Gist options
  • Save immkavin-ranks/41ff6fa76b3a76f9dfb4c1b2964a85a7 to your computer and use it in GitHub Desktop.
Save immkavin-ranks/41ff6fa76b3a76f9dfb4c1b2964a85a7 to your computer and use it in GitHub Desktop.
Prints the alternate items/elements in a list
# sample list
a_list = [1, 65, 4, 2, 3, 9, 4, 8, 4, 20, 34, 6, 15, 17, 0, 23, 3]
print('Prints alternate items from first item:')
for item in a_list:
if a_list.index(item) % 2 == 0: # (a_list.index(item) + 1) % 2 != 0:
print(item)
print('Prints alternate items from second item:')
for item in a_list:
if (a_list.index(item) + 1) % 2 == 0:
print(item)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment