Created
December 26, 2018 21:57
-
-
Save bbookman/1bd5d6ef798e1c56cd00db70e8661759 to your computer and use it in GitHub Desktop.
Python List Comprehension: Get index and value from 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
''' | |
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) |
test_string = ['hi',4, 9,8.99,'apple',('t','b','n')]
Printed as a dictionary
Dictionary = {i:test_string[i] for i in range(len(test_string))}
print(Dictionary)
List = [x for x in enumerate(test_string)]
print(List)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
`items = ["hi", 4, 8.99, 'apple', ('t,b','n')]
new_list = [(items.index(j),j) for j in items]
print(new_list)`