Would you use this function

Would you use this function
if so what would you use for

def switch(list,index_one,index_two):
  """
  1st attribute represents the list to switch
  2nd attribute represents the index to switch
  3rd attribute represent the index to switch with
  """
  items = [list[index_one],list[index_two]]#put the two index's in a seperate list
  list[index_one] = items[1]#switches the index's to the selected indexs in list
  list[index_two] = items[0]
  return list#returns the new switched list
print(switch(["World","Hello"],0,1))#in this case the output would be ['Hello', 'World']
  • Yes
  • No

0 voters

There is a simpler way to do this:

def switch(list):
    list[0],list[1] = list[1],list[0]
    return list

print(switch(["World","Hello"]))
4 Likes

Lol thanks im an idiot