0

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

4
  • People or lists, don't judge them. Commented Dec 24, 2014 at 1:19
  • ...or the list might judge you back Commented Dec 24, 2014 at 1:19
  • 1
    Wait, what do you mean by "the first list in the dict"? dicts aren't ordered. Commented Dec 24, 2014 at 1:22
  • the [] part in {u'Three': []} a guy answer my question ,I can use if not any(movietimes.values()) but it's deleted Commented Dec 24, 2014 at 1:31

1 Answer 1

1
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 
Sign up to request clarification or add additional context in comments.

2 Comments

so I have to type ["ONE"] ?? can it be catch by code . because I have many of them ["ONE"] ["Two"] ["Three"] ....
you can but it depends how your dict is structured

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.