2
dob_list = ['01-Jan-1990', '11-Aug-1995', '15-Apr-1982', '20-Mar-1988', '25-Nov-1976', '07-Dec-1965', 
         '18-Dec-1977', '25-May-1994', '09-Oct-1981', '19-Feb-1981']

I have split up each element within the list to obtain the year

how do i deduct the birth year from current year? (Eg 2021-1990)

results to be copied into the empty dictionary age[]

Heres my code:

year = 2021
age =[]
dob = []

for x in dob_list:
    age +=x[7:11].split("-")
print(age)
sj95126
  • 6,520
  • 2
  • 15
  • 34
pot
  • 37
  • 3
  • 3
    Why are you splitting on `':'` rather than `'-'`? – John Coleman Oct 14 '21 at 13:09
  • 1
    current year minus birth year doesn't always give you the correct age – Sayse Oct 14 '21 at 13:10
  • you have indentation issues. Moreover, age is a list, you should append items to it. – scandav Oct 14 '21 at 13:12
  • ok i have done some edits to the code. The age list is meant to append the values derived from subtracting 2021 to the respective birth year in the list. Thanks ! – pot Oct 14 '21 at 13:15
  • 1
    Code edits should just be to fix formatting issues so that the posted code matches your actual code. Changing your actual code so that you have partially debugged it makes your question a moving target. Please try to avoid that. – John Coleman Oct 14 '21 at 13:17

4 Answers4

4

I suggest using datetime module in order to be able to compute the exact date and avoid splitting issues of your date strings.

The function calculate_age allows you to compute the exact age of a person. Credits to this answer.

Below is the full code.

import datetime

dob_list = ['01-Jan-1990', '11-Aug-1995', '15-Apr-1982', '20-Mar-1988', '25-Nov-1976', '07-Dec-1965', '18-Dec-1977', '25-May-1994', '09-Oct-1981', '19-Feb-1981']

date_now = datetime.datetime.now()

age =[]

def calculate_age(date_now, born_date):
    return date_now.year - born_date.year - ((date_now.month, date_now.day) < (born_date.month, born_date.day))

for x in dob_list:
    date = datetime.datetime.strptime(x, '%d-%b-%Y')
    age.append(calculate_age(date_now, date))

print(age)
# Output: [31, 26, 39, 33, 44, 55, 43, 27, 40, 40]
scandav
  • 749
  • 1
  • 7
  • 21
3

You can use datetime and relativedelta from the dateutil package like below:

(you need to install pip install python-dateutil)

>>> from datetime import datetime
>>> from dateutil.relativedelta import relativedelta

>>> dob_list = ['01-Jan-1990', '11-Aug-1995', '15-Apr-1982', '20-Mar-1988', '25-Nov-1976', '07-Dec-1965', '18-Dec-1977', '25-May-1994', '09-Oct-1981', '19-Feb-1981']

>>> [relativedelta(datetime.now(),datetime.strptime(dob, '%d-%b-%Y')).years for dob in dob_list]
[31, 26, 39, 33, 44, 55, 43, 27, 40, 40]

# for more explanation
>>> [relativedelta(datetime.now(),datetime.strptime(dob, '%d-%b-%Y')) for dob in dob_list]

[relativedelta(years=+31, months=+9, days=+13, hours=+16, minutes=+50, seconds=+14, microseconds=+585158),
 relativedelta(years=+26, months=+2, days=+3, hours=+16, minutes=+50, seconds=+14, microseconds=+585268),
 relativedelta(years=+39, months=+5, days=+29, hours=+16, minutes=+50, seconds=+14, microseconds=+585325),
 relativedelta(years=+33, months=+6, days=+24, hours=+16, minutes=+50, seconds=+14, microseconds=+585379),
 relativedelta(years=+44, months=+10, days=+19, hours=+16, minutes=+50, seconds=+14, microseconds=+585426),
 relativedelta(years=+55, months=+10, days=+7, hours=+16, minutes=+50, seconds=+14, microseconds=+585469),
 relativedelta(years=+43, months=+9, days=+26, hours=+16, minutes=+50, seconds=+14, microseconds=+585504),
 relativedelta(years=+27, months=+4, days=+19, hours=+16, minutes=+50, seconds=+14, microseconds=+585546),
 relativedelta(years=+40, days=+5, hours=+16, minutes=+50, seconds=+14, microseconds=+585587),
 relativedelta(years=+40, months=+7, days=+25, hours=+16, minutes=+50, seconds=+14, microseconds=+585620)]
I'mahdi
  • 23,382
  • 5
  • 22
  • 30
2

You can simply get the last 4 characters of each string like this:

for i in dates:
    date = i[-4:]

But you must remember that year1-year2 doesn't return an age.

datetime


Otherwise, you can use the datetime module in the scope of getting the date, in particular a datetime.datetime.date object.
The input format should be something like dd-mm-yyyy.

from datetime import datetime

You can find a tutorial about this module here, and the documentation there.
Then you can look at this answer to better understand how to get how many year passed between two dates (in that case your date and datetime.datetime.now()).

FLAK-ZOSO
  • 3,873
  • 4
  • 8
  • 28
2

If you dont want to use date time, and all you care about is the birth year, then you can simply convert it to an Integer and subtract it from the birth year.

year = 2021
age = [year - int(birthdDate[-4]) for birthDate in dob_list]
Aditya Landge
  • 1,177
  • 1
  • 10
  • 18