user1481793
user1481793

Reputation: 553

Python: create list of tuples

I am trying to create a list of tuples in django like

appointments = [(datetime(2012, 5, 22, 10), datetime(2012, 5, 22, 10, 30)),    
                (datetime(2012, 5, 22, 12), datetime(2012, 5, 22, 13)),    
                (datetime(2012, 5, 22, 15, 30), datetime(2012, 5, 22, 17, 10))]

I am iterating a django query set and storing the value in appoinments list like

    appoinments = [] 
    for select_meeting in get_meeting:
        getm = int(select_meeting.duration)
        appoinments += zip(((select_meeting.meeting_datetime),
                            (select_meeting.meeting_datetime + timedelta(minutes = getm))))
    print appoinments

But it's returning the result like that's not my requirement actually

[(datetime.datetime(2012, 11, 11, 21, 5),),
 (datetime.datetime(2012, 11, 11, 22, 5),),
 (datetime.datetime(2012, 11, 11, 23, 5),),
 (datetime.datetime(2012, 11, 12, 0, 5),)]

Upvotes: 0

Views: 3281

Answers (2)

Aesthete
Aesthete

Reputation: 18848

appointments = [(x, x+timedelta(minutes=x.duration)) for x in get_meeting]

Upvotes: 0

jasisz
jasisz

Reputation: 1298

You are using zip wrong - it is actually how it works and that's normal (i-th tuple it returns contains i-th element of given iterable). Those line should be:

appointments.append((select_meeting.meeting_datetime,
                     select_meeting.meeting_datetime + timedelta(minutes = getm)))

and it should be what you want now.

Upvotes: 2

Related Questions