My input is in the form of pairs of comma-separated values, e.g.,
805i,9430 3261i,9418 3950i,9415 4581i,4584i 4729i,9421 6785i,9433 8632i,9434 9391i,9393i and I want to read them into a list of pair of string. The below does the job for a given line in open(<filename>,'r')
bs = line.strip().split() bss = [] for b in bs : x, y = b.split(',') bss.append((x,y)) However is there a way I can do this in one line with a list comprehension? Note: that I could do [(b.split(',')[0], b.split(',')[1]) for b in bs], but this unnecessarily calls the split function twice.
split()and you would need only first two values then you could use slicingtuple( b.split(',')[:2] )