Skip to content

Instantly share code, notes, and snippets.

@bbookman
Created December 26, 2018 21:57
Show Gist options
  • Select an option

  • Save bbookman/1bd5d6ef798e1c56cd00db70e8661759 to your computer and use it in GitHub Desktop.

Select an option

Save bbookman/1bd5d6ef798e1c56cd00db70e8661759 to your computer and use it in GitHub Desktop.
Python List Comprehension: Get index and value from list
'''
Get the index and the value as a tuple for items in the list ["hi", 4, 8.99, 'apple', ('t,b','n')]. Result would look like [(index, value), (index, value)]
'''
items = ["hi", 4, 8.99, 'apple', ('t,b','n')]
result = [(index, item) for index, item in enumerate(items)]
print(result)
@vyeos
Copy link

vyeos commented Aug 25, 2024

given = ["hi", 4, 8.99, 'apple', ('t,b','n')]
lis1 = []
for i in given:
lis1.append((given.index(i), i))
print(lis1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment