Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have to combine two lists using the dict function and have them go in order alphabetically, and the hire dates have to match up, so it looks like this
Alina : Dec. 24, 2013
Carly : Aug. 09, 2012
Cory : Sep. 19, 2014
Danny : Dec. 02, 2014
Flor : Jan. 19, 2010
Florrie : Feb. 13, 2019
Gennie : Nov. 02, 2016
Kathlene : May 19, 2014
Kenna : Apr. 02, 2016
Lavette : Mar. 06, 2010
Leontine : Dec. 22, 2016
Madalyn : Apr. 30, 2017
Maricela : Mar. 24, 2016
Minna : Jul. 31, 2018
Monica : Jul. 23, 2013
Monte : Nov. 23, 2010
Nancey : Sep. 02, 2011
Olympia : Jul. 9, 2014
Rikki : Aug. 10, 2017
Yolando : Feb. 21, 2019

my code looks like this:
['Alina', 'Carly', 'Cory', 'Danny', 'Flor', 'Florrie', 'Gennie', 'Kathlene', 'Kenna', 'Lavette', 'Leontine', 'Madalyn', 'Maricela', 'Minna', 'Monica', 'Monte', 'Nancey', 'Olympia', 'Rikki', 'Yolando'] ['Apr. 02, 2016', 'Apr. 30, 2017', 'Aug. 09, 2012', 'Aug. 10, 2017', 'Dec. 02, 2014', 'Dec. 22, 2016', 'Dec. 24, 2013', 'Feb. 13, 2019', 'Feb. 21, 2019', 'Jan. 19, 2010', 'Jul. 23, 2013', 'Jul. 31, 2018', 'Jul. 9, 2014', 'Mar. 06, 2010', 'Mar. 24, 2016', 'May 19, 2014', 'Nov. 02, 2016', 'Nov. 23, 2010', 'Sep. 02, 2011', 'Sep. 19, 2014'] {'Olympia': 'Jul. 9, 2014', 'Alina': 'Dec. 24, 2013', 'Kenna': 'Apr. 02, 2016', 'Yolando': 'Feb. 21, 2019', 'Leontine': 'Dec. 22, 2016', 'Flor': 'Jan. 19, 2010', 'Gennie': 'Nov. 02, 2016', 'Madalyn': 'Apr. 30, 2017', 'Kathlene': 'May 19, 2014', 'Nancey': 'Sep. 02, 2011', 'Monica': 'Jul. 23, 2013', 'Maricela': 'Mar. 24, 2016', 'Danny': 'Dec. 02, 2014', 'Cory': 'Sep. 19, 2014', 'Rikki': 'Aug. 10, 2017', 'Monte': 'Nov. 23, 2010', 'Minna': 'Jul. 31, 2018', 'Florrie': 'Feb. 13, 2019', 'Lavette': 'Mar. 06, 2010', 'Carly': 'Aug. 09, 2012'}


What I have tried:

names_list = ['Olympia', 'Alina', 'Kenna', 'Yolando', 'Leontine',
'Flor', 'Gennie', 'Madalyn', 'Kathlene', 'Nancey',
'Monica', 'Maricela', 'Danny', 'Cory', 'Rikki',
'Monte', 'Minna', 'Florrie', 'Lavette', 'Carly' ]

hiredate_list = [ 'Jul. 9, 2014', 'Dec. 24, 2013', 'Apr. 02, 2016',
'Feb. 21, 2019',
'Dec. 22, 2016', 'Jan. 19, 2010', 'Nov. 02, 2016',
'Apr. 30, 2017', 'May 19, 2014', 'Sep. 02, 2011',
'Jul. 23, 2013', 'Mar. 24, 2016', 'Dec. 02, 2014',
'Sep. 19, 2014', 'Aug. 10, 2017', 'Nov. 23, 2010',
'Jul. 31, 2018', 'Feb. 13, 2019',
'Mar. 06, 2010', 'Aug. 09, 2012' ]

zip_iterator = zip(names_list, hiredate_list)

a_dictionary = dict(zip_iterator)

sorted_list = sorted(names_list)
sorted_list2 = sorted(hiredate_list)

print(sorted_list, sorted_list2, a_dictionary)
Posted
Updated 29-Sep-21 5:14am
v2
Comments
Richard MacCutchan 29-Sep-21 11:05am    
And what happened?
Sagar Bulsara 29-Sep-21 11:07am    
the code comes out like this
['Alina', 'Carly', 'Cory', 'Danny', 'Flor', 'Florrie', 'Gennie', 'Kathlene', 'Kenna', 'Lavette', 'Leontine', 'Madalyn', 'Maricela', 'Minna', 'Monica', 'Monte', 'Nancey', 'Olympia', 'Rikki', 'Yolando'] ['Apr. 02, 2016', 'Apr. 30, 2017', 'Aug. 09, 2012', 'Aug. 10, 2017', 'Dec. 02, 2014', 'Dec. 22, 2016', 'Dec. 24, 2013', 'Feb. 13, 2019', 'Feb. 21, 2019', 'Jan. 19, 2010', 'Jul. 23, 2013', 'Jul. 31, 2018', 'Jul. 9, 2014', 'Mar. 06, 2010', 'Mar. 24, 2016', 'May 19, 2014', 'Nov. 02, 2016', 'Nov. 23, 2010', 'Sep. 02, 2011', 'Sep. 19, 2014'] {'Olympia': 'Jul. 9, 2014', 'Alina': 'Dec. 24, 2013', 'Kenna': 'Apr. 02, 2016', 'Yolando': 'Feb. 21, 2019', 'Leontine': 'Dec. 22, 2016', 'Flor': 'Jan. 19, 2010', 'Gennie': 'Nov. 02, 2016', 'Madalyn': 'Apr. 30, 2017', 'Kathlene': 'May 19, 2014', 'Nancey': 'Sep. 02, 2011', 'Monica': 'Jul. 23, 2013', 'Maricela': 'Mar. 24, 2016', 'Danny': 'Dec. 02, 2014', 'Cory': 'Sep. 19, 2014', 'Rikki': 'Aug. 10, 2017', 'Monte': 'Nov. 23, 2010', 'Minna': 'Jul. 31, 2018', 'Florrie': 'Feb. 13, 2019', 'Lavette': 'Mar. 06, 2010', 'Carly': 'Aug. 09, 2012'}
Richard MacCutchan 29-Sep-21 11:15am    
See my solution below.

1 solution

You do not need to sort the two lists. Once you have created the dictionary you can print it thus:
Python
for name, date in sorted(a_dictionary.items()):
    print(F"{name:10} {date}")

Output:
Alina      Dec. 24, 2013
Carly      Aug. 09, 2012
Cory       Sep. 19, 2014
Danny      Dec. 02, 2014

etc.
 
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