CS2613/tests/Final/Q2 Python/invert.py
2022-12-20 21:49:28 -04:00

20 lines
615 B
Python

def invert(lst):
lst = list(lst) # Makes sure arugment is a list
#list_range = range(0, len(lst)) # Code I might have used if I used a dictionary comprehension
ret_dict = dict() # Create empty dict
counter = 0 # Create counter
for i in lst:
ret_dict[i] = counter # Assign each element of list to
# its postion in list
counter = counter + 1 # Increment counter
if (len(lst) > len(ret_dict)): # Check if the length of new dict is less than
return None # input list, if so, there is duplicates so return none
return ret_dict # Return created dictionary