user2492364
user2492364

Reputation: 6713

How to tell if a list inside a dict is empty

I have a movietimes={}

It a dict I make by this code:

for i in Showtime.objects.filter(movie_id=movieid,theater_id=theaterid,datetime__range=(today,tomorrow))):
    if i.mvtype not in movietimes:
        movietimes[i.mvtype] = []
    if not i.movietime > today :
        movietimes[i.mvtype].append(i.movietime.strftime('%Y-%m-%dT%H:%M:%S.%fZ'))

if not movietimes : #this have bug
    for i in Showtime.objects.filter(movie_id=movieid,datetime__range=(yesterday,today)):
        if i.mvtype not in movietimes:
            movietimes[i.mvtype] = []
        movietimes[i.mvtype].append(i.movietime.strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
return movietimes

result like this:

     "Times": {
                "ONE: [
                    "2014-12-24T10:40:00.000000Z", 
                    "2014-12-24T12:45:00.000000Z", 
                    "2014-12-25T14:50:00.000000Z"
                ]
            }

I want to ask how can't I judge that if the [] in the 'ONE' part is null ??

I can't use if not movietimes={}: ,because there is u'ONE': [] in the dict

I have to judge the first list in the dict is empty . And there are many types u'ONE',u'TWO',u'Three'

they are catch by i.mvtype

{u'ONE': []}
{u'TWO': []}
{u'Three': []}

Please help me ,Thank you

Upvotes: 0

Views: 57

Answers (1)

Padraic Cunningham
Padraic Cunningham

Reputation: 180550

if not movietimes["Times"]["ONE"]:
    # you have empty list

That is presuming by first you mean the key ONE as dicts are not ordered

If you want to see if there is any empty list and your dict is like below:

movietimes = {"Times":{"ONE":[2],"TWO":[]}}

for val in movietimes["Times"].itervalues():
   if not any(x for x in val):
        # you have empty list

Upvotes: 1

Related Questions