Iterating over tuple entries in a list
Peter Otten
__peter__ at web.de
Thu Mar 11 02:46:20 EST 2004
Dirk Zimmermann wrote:
> Hi,
>
> I have the following problem:
>
> I have a list with tuples in it, looking like this:
>
> myList = [(1.1, "file1"), (1.2,"file2), (1.9, "file3")]
>
> (I want to store a number calculated from a file together with the
> file.) Than I want to use functions expecting a list of floats. Is there
> an easy way to slice myList? I know, that:
>
> myList[2][0]
>
> gives:
> 1.9
>
> But:
>
> myList[0:2][0]
>
> does _not_ give something like:
>
> [1.1, 1.2, 1.9]
>
> By now I loop over the list, select the numbers and store them in a new
> list. But I don't like that way very much. Is there a "better" way?
If you are really lazy:
>>> myList = [(1.1, "file1"), (1.2, "file2"), (1.9, "file3")]
>>> zip(*myList)[0]
(1.1000000000000001, 1.2, 1.8999999999999999)
>>>
Peter
More information about the Python-list
mailing list