'simplify python print

Suppose I have a list:

row = [u'28d07ef48e40', u'373ac79f615f', u'a3ec4faddbec', u'c0195f9568c6', u'cc4ebc7b826c', u'ccdfdb826c', u'cc4fa826c', u'cc4eeesb826c', u'ccfesb826c']

my print is

fw.write("%s %s <%s> [%s] %s %s (%s) %s: %s" %(str(row[0]),str(row[1]),str(row[2]),str(row[3]),str(row[4]),str(row[5]),str(row[6]),str(row[7]),str(row[8])))

How can I simplify this python print?



Solution 1:[1]

you can try using the join function

row = [u'28d07ef48e40', u'373ac79f615f', u'a3ec4faddbec', u'c0195f9568c6', u'cc4ebc7b826c', u'ccdfdb826c', u'cc4fa826c', u'cc4eeesb826c', u'ccfesb826c']

fw.write(' '.join(row))

this works beacuse the join function joins everything in the list into the string

Solution 2:[2]

The format string is in it's simplest form. What you can "simplify" is the conversion to string:

"%s %s <%s> [%s] %s %s (%s) %s: %s".format(map(str, row))

map(str, row) will call str() on all elements n row, and returns a list in 2.7, and iterator in 3.7.

Solution 3:[3]

The *row operator will pack the list

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 handras
Solution 3 kirikoumath