3

I would like to print the list in one line. I works with simple print, but it doesn´t work when it´s f print. How could I do that, please? Thank you

list = ["A", "B", "C"]

print(*[n for n in list], sep=", ")

print(f"Item was added: {*[n for n in nazvy]}")
  • You can make it even simpler. `print(*list, sep=", ")` will give you `A, B, C` – Joe Ferndz Mar 26 '21 at 17:01
  • Does this answer your question? [f-string syntax for unpacking a list with brace suppression](https://stackoverflow.com/questions/42756537/f-string-syntax-for-unpacking-a-list-with-brace-suppression) – Joe Ferndz Mar 26 '21 at 17:18

4 Answers4

3

Use ' '.join

l = ["A", "B", "C"]
print(', '.join(l))
print(f"Item was added: {', '.join(l)}")

Output

A, B, C
Item was added: A, B, C

NOTE: Try not to use in-built keywords as variable names. Eg: list


Your code didn't work because f-string doesn't allow starred expression or unpacking.

Meanwhile

print(*[n for n in list], sep=", ")

worked because it translates to below due to list unpacking

print('A', 'B', 'C', sep=", ")
Vishnudev Krishnadas
  • 10,679
  • 2
  • 23
  • 55
3

I'd suggest this :

list = ["A", "B", "C"]
print(f'Item was added: {",".join(str(x) for x in list)}')
print(f'Item was added: {", ".join(str(x) for x in list)}')

as per accepted answer on this question : f-string syntax for unpacking a list with brace suppression

irnerd
  • 411
  • 2
  • 10
  • How does `str(x)` add any value? – Vishnudev Krishnadas Mar 24 '21 at 10:12
  • @Vishnudev - str(x) is belt and braces - defensive coding - so if the list contained a numeric, each iteration is processed uniformly and cleanly. I'd suggest it is wise to never assume the data is one structure or another or there will be many rewrites down the track – irnerd Mar 25 '21 at 10:35
  • I agree with you on this. But for this question, where the OP has given a list with identical data structure i.e. `str` It will create a minimal time overhead. Also, there is no one code that fits all here because an item in a list might very well be another list. There are no boundaries to considering all possibilities. Hence, we follow the KISS(Keep it simple stupid) rule for what we have right now. Good thinking btw. :) – Vishnudev Krishnadas Mar 26 '21 at 02:08
1

Use "end=" instead of "sep=";

list = ["A", "B", "C"]
for x in list:
   print(x, end=', ')
TudorTeo
  • 133
  • 2
  • 12
0

You can use *list to send each value to print statement. With this approach, you don't have to worry about converting data to string.

With that, you can just give

mylist = ["A", "B", "C", 4, 5, 6]

print(*my_list, sep=", ")

The output of this will be:

A, B, C, 4, 5, 6

If you want to use the fstring, you can also try giving:

print(f'{*mylist,}')

The output of this will be:

('A', 'B', 'C', 4, 5, 6)

Note here that it prints with brackets ().

For more details about unpacking f-string, see the post on Stackoverflow here

Also please try to avoid naming variables as list. It will get confusing later when you want to convert data to a list.

Example:

x = {1:5,2:10,3:15}
y = list(x)
print (y)

will convert dictionary x to a list y

Joe Ferndz
  • 8,417
  • 2
  • 13
  • 33