John
John

Reputation: 145

Print sublists in python

I am trying to print a list of lists in python like so:

for location in latLongList:
    print ' '.join(map(str, location))

This prints out:

40.0349216312 -75.1900864349 Paved 4 0.156150432289
39.9531308619 -75.1629612614 Paved 3 0.170932927052
39.9610355788 -75.1725011285 Paved  0.17296824247
39.9788367755 -75.2123945669 Paved  0.196740550111
39.9467944475 -75.2092212039 Paved 33 0.210834020854
39.94626513 -75.2089212417 Paved 5 0.210899309368
39.9373184367 -75.2341880089 Grass  0.236747322815
39.9413269464 -75.2383849209   0.238056333485

This works fine but I wanted to exclude the last number in each line (which is the last number in each sublist). I also wanted to be able to allow the user to specify the number of lines to be printed. They input that number through the command line and it is stored in a variable called sizeOfList. Would there be an easy way to do this in python? Any help would be greatly appreciated!

Upvotes: 2

Views: 1665

Answers (5)

pzp
pzp

Reputation: 6597

This should solve both problems:

print '\n'.join('{} {} {}'.format(*location) for location in latLongList[:sizeOfList])

This solution is pretty Pythonic. Using str.format() eliminates the need to convert everything to strings by using map(str, ) (str.format() does this automatically for you). Additionally, there is no need to slice the sublist for each location to eliminate the last element, as str.join() ignores the rest of the list automatically. Lastly, it reduces the number of calls to print from sizeOfList times to once. And the str.join() operation is fast because it is joining an iterator instead of a list.

Enjoy!

Upvotes: 0

abcd
abcd

Reputation: 10751

You could use the built-in function enumerate to get the index of each location in latLongList, and then print only locations whose index is less than the number desired by the user (sizeOfList). Then, in order to exclude the last item in each sublist (each location), you could take a slice of the sublist up to, but not including, the last item (which is at index -1).

for i, location in enumerate(latLongList):
    if i < sizeOfList:
        print ' '.join(map(str, location[:-1]))

@Hackaholic introduced an improvement to this method, which makes the code more concise and potentially much faster (due to iteration over fewer locations):

for location in latLongList[:sizeOfList]:
    print ' '.join(map(str, location[:-1]))

Here, only the items up to the number desired by the user (sizeOfList) are taken from latLongList. There is no longer a need for enumerate.

Upvotes: 3

Hackaholic
Hackaholic

Reputation: 19733

Better to try like this:

for location in latLongList[:sizeOfList]:
    print ' '.join(map(str, location[:-1]))

Upvotes: 0

abarnert
abarnert

Reputation: 365677

First, to print everything but the last element, you slice the list to include everything but its last element: location[:-1]. (Slicing is explained in the tutorial in Using Python as a Calculator; for full details see Sequence Types in the library reference.)

Then, to stop after a certain number of lines, you slice the list of lines: latLongList[:sizeOfList].

So:

for location in latLongList[:sizeOfList]:
    print ' '.join(map(str, location[:-1]))

If the list weren't actually a list, but an iterator (or if you were writing generic code that needed to work with both), or if you were worried about the memory waste in making a copy of part of the list (say because you want the first 200 million lines), you could use islice, which solves both of those problems:

from itertools import islice
for location in islice(latLongList, sizeOfList):
    print ' '.join(map(str, location[:-1]))

Upvotes: 0

Blender
Blender

Reputation: 298136

You could do something like this:

# this import needs to be first
from __future__ import print_function

for location in latLongList[:sizeOfList]:
    print(*location[:-1])

The __future__ import makes print a function, so you can do print(*foo). That's like print(foo[0], foo[1], ...).

Upvotes: 1

Related Questions