Click here to Skip to main content
15,791,963 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wonder How I can make certain character always comes first in Python.

For example, I have a list as following.

A = ['a', 'c', 'b', 'x', 'f', 'z'] 


Is there any way that I can make "x" always comes first?

A = ['x', 'a', 'c', 'b', 'f', 'z']


What I have tried:

I have tried IF.

if 'f' in A:
    print('f' + A)


I know this is not a good idea. Would you please let me know how to do it?
Posted
Updated 22-Feb-20 23:54pm
Comments
Gerry Schmitz 23-Feb-20 0:01am    
join ('f' from A) with (all A except 'f')

1 solution

Python
A = ['x', 'a', 'c', 'b', 'f', 'z']
B = ['f'] + [x for x in A if x != 'f']
print(B)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900