-1
paid_students=[]
for students in enrollments:
    if students['days_to_cancel']==None or students['days_to_cancel']>7:
        paid_students.append(students)
print len(paid_students)

Output:

1640

The value of len(enrollments) is also 1640. Why are all the rows getting appended to the paid_students list even though many rows in the enrollments have ['days_to_cancel'] values having wide ranges.

Example data for enrollments

{u'account_key': u'448',
 u'cancel_date': u'2014-11-10',
 u'days_to_cancel': u'5',
 u'is_canceled': u'True',
 u'is_udacity': u'True',
 u'join_date': u'2014-11-05',
 u'status': u'canceled'}

{u'account_key': u'448',
 u'cancel_date': u'2015-01-27',
 u'days_to_cancel': u'0',
 u'is_canceled': u'True',
 u'is_udacity': u'True',
 u'join_date': u'2015-01-27',
 u'status': u'canceled'}

Source-Udacity

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

1 Answers1

0

This works for me: i added an int() in the second parameter of if, you were comparing stings(your dic values) with integers, which in python 2 as pointed in the comments always returns true.

enrollments= [{u'account_key': u'448', u'cancel_date': u'2014-11-10', u'days_to_cancel': u'5', u'is_canceled': u'True', u'is_udacity': u'True', u'join_date': u'2014-11-05', u'status': u'canceled'},
{u'account_key': u'448', u'cancel_date': u'2015-01-27', u'days_to_cancel': u'10', u'is_canceled': u'True', u'is_udacity': u'True', u'join_date': u'2015-01-27', u'status': u'canceled'}]

paid_students=[]
for students in enrollments:
    if students['days_to_cancel']==None or int(students['days_to_cancel'])>7:
        paid_students.append(students)

print len(paid_students)