Can I count on order being preserved in a Python tuple?

I've got a list of datetimes from which I want to construct time segments. In other words, turn [t0, t1, ... tn] into [(t0,t1),(t1,t2),...,(tn-1, tn)]. I've done it this way:

# start by sorting list of datetimes
mdtimes.sort()
# construct tuples which represent possible start and end dates

# left edges
dtg0 = [x for x in mdtimes]
dtg0.pop()

# right edges
dtg1 = [x for x in mdtimes]
dtg1.reverse()
dtg1.pop()
dtg1.sort()

dtsegs = zip(dtg0,dtg1)

Questions...

  1. Can I count on tn-1 < tn for any (tn-1,tn) after I've created them this way? (Is ordering preserved?)
  2. Is it good practice to copy the original mdtimes list with list comprehensions? If not how should it be done?
  3. The purpose for constructing these tuples is to iterate over them and segment a data set with tn-1 and tn. Is this a reasonable approach? i.e.

    datasegment = [x for x in bigdata if ( (x['datetime'] > tleft) and (x['datetime'] < tright))] 
    

Thanks

Answers


  1. Tuple order is as you insert values into the tuple. They're not going to be sorted as I think you're asking. zip will again, retain the order you inserted the values in.

  2. It's an acceptable method, but I have 2 alternate suggestions: Use the copy module, or use dtg1 = mdtimes[:].

  3. Sounds reasonable.


Both list and tuple are ordered.

dtg0, dtg1 = itertools.tee(mdtimes)
next(dtg0)
dtsegs = zip(dtg0, dtg1)

Need Your Help

zf2 Form Bind Object Issue

forms binding zend-framework2

I have a problem with zf2, form bind object procedures, to put it short I am trying to automate data exchange to my entity from form object once form validation is through, for which I had implemen...

How to target Debian stable but develop on testing?

linux debian cross-compiling chroot binary-compatibility

We have some C++ applications which are developed specifically and only for Debian stable (x86).

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.