-1

I'm building a Formula1 race simulator in python and I'm trying to make a overtake function, basically, i've all the drivers stored in a list and once one of the drivers surpasses the other I need to invert their position in the list

['hamilton','vertsappen','perez','sainz']
['hamilton','perez','verstappen','sainz']

is there any way to do so?

since now I have tried to store the original positions in a temporary variable but I keep finding myself with duplicates in the list

original temporary variables

overtaken temp = Valteri Bottas
overtaker temp = Nicholas Latifi

after the inverting

overtaken temp = Valteri Bottas
overtaker temp = Valteri Bottas
avocadoLambda
  • 1,332
  • 7
  • 16
  • 33
  • can you mutate the existing list or do you need to create a new one and keep the original position as-is? – hlin03 Dec 04 '22 at 11:32

2 Answers2

1

A lazy way to do it.

a = ['hamilton','vertsappen','perez','sainz']

a.remove('vertsappen')

a.insert(2, 'vertsappen')

print(a)

#['hamilton','perez','verstappen','sainz']
ricogordo
  • 36
  • 2
0

i solved the problem, it was much easier then i thought:

grid[0], grid[1] = grid[1] = grid[0]