I have list of list and I have this code in which I am accessing certain elements of the list and adding an increasing number to serve as an index. My sample list looks like this:
list1 = [['item1', '100', '07/05/2017'], ['item2', '115', '07/07/2017']....]
from this list I want to select first two element of the list and print like this(Note that I don't want to delete the last column from the list but just not print it):
1. ITEMS: item1, PRICE: 100 2. ITEMS: item2, PRICE: 115
I wrote this line of code to do that:
inner_list_str = ["%d. ITEMS: %s, PRICE: %s" % (i, *x) for i, x in enumerate(list1, 1)]
but I get error as: TypeError: not all arguments converted during string formatting. I think its because of 3 columns being in each element of my list but only 2 %s. How can I resolve it?
.format()instead. It's cleaner. (Also, your hunch is correct: only two%s's.)