Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unable to Loop Over Date
#1
Hello, I'm trying to loop over a dictionary that contains date but as I do so I'm unable to print out the date, can you help me on this and possibly explain why? Thanks

Below is my code snippet:
from datetime import date

my_dates = {"Jane": str(date(1994, 12, 25)), "Daniel": str(date(2000, 11, 13)), "George": str(date(1971, 9, 27))}
for mdate in my_dates:
    print(mdate)
Output:
C:\Users\...\Desktop\coding>python dict_date.py
Jane
Daniel
George
Reply
#2
Iterating over a dictionary means iterating over the keys. Use those to get the values, or use the values method if you want just the values, or items if you want both keys and values.
Reply
#3
Examples:

#! /usr/bin/env python3

from datetime import date

my_dates = {"Jane": str(date(1994, 12, 25)), "Daniel": str(date(2000, 11, 13)), "George": str(date(1971, 9, 27))}

text = 'Using key, value pair'
print(f'{text}')
print('-'*len(text))
for name, date in my_dates.items():
    print(f'Name: {name} Date: {date}')
print()

text = 'Using only key'
print(text)
print('-'*len(text))
for name in my_dates.keys():
    print(f'Name: {name}')
print()

text = 'Using only value'
print(text)
print('-'*len(text))
for date in my_dates.values():
    print(f'Date: {date}')
Output:
Using key, value pair --------------------- Name: Jane Date: 1994-12-25 Name: Daniel Date: 2000-11-13 Name: George Date: 1971-09-27 Using only key -------------- Name: Jane Name: Daniel Name: George Using only value ---------------- Date: 1994-12-25 Date: 2000-11-13 Date: 1971-09-27
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare current date on calendar with date format file name Fioravanti 1 214 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Python date format changes to date & time 1418 4 588 Jan-20-2024, 04:45 AM
Last Post: 1418
  Date format and past date check function Turtle 5 4,233 Oct-22-2021, 09:45 PM
Last Post: deanhystad
  How to add previous date infront of every unique customer id's invoice date ur_enegmatic 1 2,227 Feb-06-2021, 10:48 PM
Last Post: eddywinch82
  Unable to print stuff from while loop Nick1507 4 2,342 Sep-17-2020, 02:26 PM
Last Post: Nick1507
  How to add date and years(integer) to get a date NG0824 4 2,855 Sep-03-2020, 02:25 PM
Last Post: NG0824
  Unable to pass date timestamp as Parameters : nagu4651 1 1,813 Jun-22-2020, 06:07 PM
Last Post: Larz60+
  Unable to combine print statements in for loop adeana 2 1,989 Jun-12-2020, 05:08 PM
Last Post: adeana
  Unable to execute the for loop harold 1 1,727 Sep-23-2019, 12:27 PM
Last Post: buran
  Substracting today's date from a date in column of dates to get an integer value firebird 1 2,126 Jul-04-2019, 06:54 PM
Last Post: Axel_Erfurt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020