7

I am looking to print out results from a list built in my Python program.

I will take the results of the values in the lists and print them out separated by tabs.

I have a list called 'a' and I will just select the element to produce the final table.

But when I use the .format method, I have had to call the list four times.

a = [1,2,3,4]
print('a{[0]}\ta{[3]}\ta{[1]}\ta{[2]}'.format(a, a, a, a))

Output:

a1  a4  a2  a3

is there a better method of telling the format command to use list 'a' each time?

zeeman
  • 185
  • 1
  • 1
  • 12

4 Answers4

8

You can use the *(single asterisk) to unpack the list for you. See also: Unpack a list

a = [1,2,3,4]
print('a{0}\ta{3}\ta{1}\ta{2}'.format(*a))
Taku
  • 31,927
  • 11
  • 74
  • 85
  • print('a{0}\ta{3}\ta{1}\ta{2}'.format(*a)) worked. [0] wasn't necessary to select the element. Thanks. – zeeman Oct 19 '17 at 15:13
5

If you're using Python 3.6 an up, you can use f-strings if you like:

a = [1, 2, 3, 4]
result = f"a{a[0]}\ta{a[3]}\ta{a[1]}\ta{a[2]}"
print(result)

but it's not very extensible if a ever changes it's dimensions.

If the order didn't matter I'd be more inclined to do something more along the lines of:

a = [1, 2, 3, 4]
result = "\t".join(f"a{x}" for x in a)
print(result)

This uses a comprehension to create a sequence of strings formatted the way you want, then join them together with tabs.

If order (or only selecting certain items) is important, but you still wanted to use something like this, you could try:

a = [1, 2, 3, 4]
indexes = [0, 3, 1, 2]
result = "\t".join(f"a{a[i]}" for i in indexes)
print(result)

That way you can change the size of a, or the order you want just by changing those lists and the code will "just work" (presuming that you don't go outside the bounds of the list). So if you wanted to just print indexes 2 and 3:

a = [1, 2, 3, 4]
indexes = [2, 3]
result = "\t".join(f"a{a[i]}" for i in indexes)
print(result)
LexyStardust
  • 1,018
  • 5
  • 18
1

This doesn't directly answer your question, but I'd just join the list with a tab:

"\t".join( map(str, [1, 2, 3, 4]) ) 

This inserts a tab between each element of the list, then joins the result together as a string.

Unfortunately, the map is required here to convert each number to a string before being given to join.

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
  • This won't work directly with the OP's code - `str.join` only works on strings. It'll fail with a `TypeError`. – LexyStardust Oct 19 '17 at 15:08
  • @LexyStardust I know, I just realized that. Forgot Python is relatively strongly typed. – Carcigenicate Oct 19 '17 at 15:08
  • You can easily map them to strings first: `'\t'.join(map(str, [1,2,3,4]))` – Alvra Oct 19 '17 at 15:09
  • @Alvra already updated. Thanks. Was fiddling with QPython on my phone. Slow going. – Carcigenicate Oct 19 '17 at 15:10
  • If you don't "like" the look of map (I'm not a fan) you could also use comprehensions (which I've provided as an example in my answer). – LexyStardust Oct 19 '17 at 15:17
  • @LexyStardust I don't mind the map. It was just such a nice succinct bit before adding it. It's a little verbose looking now. All a comprehension would do is reduce a level of parentheses, which I suppose would make it less noisy. – Carcigenicate Oct 19 '17 at 15:20
1

You can index a list inside the format string and pass the argument by name so you can easily reuse it.

print('a{a[0]}\ta{a[3]}\ta{a[1]}\ta{a[2]}'.format(a=[1,2,3,4]))
Alvra
  • 353
  • 2
  • 10