Skip to content

Instantly share code, notes, and snippets.

@richardpascual
Last active June 23, 2021 17:52
Show Gist options
  • Save richardpascual/a33b9a1a472bb19495921f933aa02e52 to your computer and use it in GitHub Desktop.
Save richardpascual/a33b9a1a472bb19495921f933aa02e52 to your computer and use it in GitHub Desktop.
Looping: Print the value for every position with an odd index
# print the value for every position with an odd index.
#Write your function here
def odd_indices(lst):
new_list = []
for idx in lst:
if lst.index(idx) % 2 != 0:
new_list.append(idx)
return(new_list)
#Uncomment the line below when your function is done
print(odd_indices([4, 3, 7, 10, 11, -2]))
# Alternate solution using a range definition
def odd_indices(lst):
new_lst = []
for index in range(1, len(lst), 2):
new_lst.append(lst[index])
return new_lst
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment