Python loop to get data from every line of file and convert it to dec
Question: I'm kinda lost at the moment: I have text file with lines looking like that:

    
    
    /dev/input/event0: 0003 0035 000002ac 
    /dev/input/event0: 0003 0036 000008fb
    

There are many lines like that, and I want to make script that gets the last
hex value from every line and then writes them in file with output looking
like this:

    
    
    something someting hex_from_line_1 hex_from_line_2 
    something someting hex_from_line_3 hex_from_line_4 
    

And so on. As I'm new to Python I have some trouble with making loop which
would do this. Can You give me any guidance? (I'm not asking for whole loop,
only guidance - I'd love to learn that, not use some finished code)


Answer: A fun thing about Python's `zip` is that it will happily take the same
iterator multiple times as arguments, allowing you to pair up inputs easily.
For example:

    
    
    # For efficiency, if you're on Python 2, include this line so zip is a generator that produces pairs on demand, rather than eagerly slurping the whole file
    from future_builtins import zip
    
    with open('myinput') as f:
        # Creates a generator that produces only the final space separated value for each line (could be anything; not checking for hex)
        final_hex = (line.rsplit(None, 1)[-1] for line in f)
        # By using the same generator twice, we get the 1st, 3rd, 5th, etc. from one
        # and the 2nd, 4th, 6th, etc. from the other.
        for hexa, hexb in zip(final_hex, final_hex):
            print("something something", hexa, hexb) # Python 3 print function
            print "something something", hexa, hexb  # Python 2 print statement
    

Note: If the input data isn't an even number of lines, this will drop the
final unpaired input. You can use `itertools.zip_longest` (`izip_longest` on
Python 2) if you want the unpaired value.

