-5

I have the below list:

list = ['A', 'B', 'C', 'D', 'E', 'F']

I'm trying to create a function that would place any of the elements at the beginning of it, for instance if I choose 'E', the list would end up:

newList = ['E', 'A', 'B', 'C', 'D', 'F']

How can I achieve this?

Cœur
  • 34,719
  • 24
  • 185
  • 251
Cvg
  • 43
  • 5

2 Answers2

1

If it's a list you could use list.insert(0, 'E').

See: https://docs.python.org/2/tutorial/datastructures.html

Youri Nooijen
  • 478
  • 3
  • 12
0

You can use a second list for that:

list = ['A', 'B', 'C', 'D', 'E', 'F']
new_list = ['E']
new_list.extend(list[:list.index('E')])
new_list.extend(list[list.index('E')+1:])
Thargor
  • 1,852
  • 14
  • 24