Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
Bonsai Boy of New York Bonsai Trees and Accessories

BlueHost Web Hosting

Liquid Web iThemes WordPress Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

DreamHost Web Hosting


Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Liquid Web Cloud VPS Hosting

10,936 questions

14,843 answers

573 users

How to move a dictionary key to the beginning in Python

1 Answer

0 votes
import collections
 
dic = collections.OrderedDict([('a', 'AA'), ('b', 'BB'), ('c', 'CC'), ('d', 'DD')])

print(dic)
for key, value in dic.items():         
    print(key, value)

print()

dic.move_to_end('d', last=False) 
print(dic)
for key, value in dic.items():         
    print(key, value)



'''
run:
 
OrderedDict([('a', 'AA'), ('b', 'BB'), ('c', 'CC'), ('d', 'DD')])
a AA
b BB
c CC
d DD

OrderedDict([('d', 'DD'), ('a', 'AA'), ('b', 'BB'), ('c', 'CC')])
d DD
a AA
b BB
c CC

'''

 




answered May 11 by avibootz
...