Chris Macaluso
Chris Macaluso

Reputation: 1482

python - get a list of first values in dictionary

I have a dictionary like so:

look_up = {1: ('January', str(now.year + 1) + '-01-01', '2020-01-31', 'E'),
           2: ('February', str(now.year + 1) + '-02-01', '2020-02-29', 'F'),
           3: ('March', str(now.year + 1) + '-03-01', '2020-03-31', 'G'),
           4: ('April', str(now.year + 1) + '-04-01', '2020-04-30', 'H'),
           5: ('May', str(now.year + 1) + '-05-01', '2020-05-31', 'I'),
           6: ('June', str(now.year + 1) + '-06-01', '2020-06-30', 'J'),
           7: ('July', str(now.year + 1) + '-07-01', '2020-07-31', 'K'),
           8: ('August', str(now.year + 1) + '-08-01', '2020-08-31', 'L'),
           9: ('September', str(now.year + 1) + '-09-01', '2020-09-30', 'M'),
           10: ('October', str(now.year) + '-10-01', '2019-10-31', 'N'),
           11: ('November', str(now.year) + '-11-01', '2019-11-30', 'O'),
           12: ('December', str(now.year) + '-12-01', '2019-12-31', 'P')}

I would like to create a list from it containing just the firs value position, in this case a list of the months. How can I do this?

Upvotes: 0

Views: 137

Answers (5)

Dhaval Taunk
Dhaval Taunk

Reputation: 1672

Try:

month_list = [value[0] for key, value in look_up.items()]

Upvotes: 1

Zig Razor
Zig Razor

Reputation: 3495

You can do this in many ways, for example you take all the values of dictionary with values() function, and the with a for loop take only the first element of each tuple.

For a better guide and tips to the use of dictionary in python i give you this link

Python Dictionary

Upvotes: 1

Kalma
Kalma

Reputation: 147

Try:

my_list = []
for entry in look_up.keys():
    my_list.append(look_up[entry][0])

print(my_list)

Upvotes: 1

alex067
alex067

Reputation: 3281

date_list = [ value[0] for value in look_up.values()]

Upvotes: 5

Alexandr Shurigin
Alexandr Shurigin

Reputation: 3981

Here we are

from datetime import datetime

now = datetime.now()

look_up = {1: ('January', str(now.year + 1) + '-01-01', '2020-01-31', 'E'),
           2: ('February', str(now.year + 1) + '-02-01', '2020-02-29', 'F'),
           3: ('March', str(now.year + 1) + '-03-01', '2020-03-31', 'G'),
           4: ('April', str(now.year + 1) + '-04-01', '2020-04-30', 'H'),
           5: ('May', str(now.year + 1) + '-05-01', '2020-05-31', 'I'),
           6: ('June', str(now.year + 1) + '-06-01', '2020-06-30', 'J'),
           7: ('July', str(now.year + 1) + '-07-01', '2020-07-31', 'K'),
           8: ('August', str(now.year + 1) + '-08-01', '2020-08-31', 'L'),
           9: ('September', str(now.year + 1) + '-09-01', '2020-09-30', 'M'),
           10: ('October', str(now.year) + '-10-01', '2019-10-31', 'N'),
           11: ('November', str(now.year) + '-11-01', '2019-11-30', 'O'),
           12: ('December', str(now.year) + '-12-01', '2019-12-31', 'P')}

print([x[0] for x in look_up.values()])

Output

python test123.py
['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']

Upvotes: 4

Related Questions