Shortest solution
Just add a comma after the unpacked list.
>>> a = [1, 2, 3]
>>> print(f"Unpacked list: {*a,}")
Unpacked list: (1, 2, 3)
There is a longer explanation to this syntax in this thread.
Caveat
With this solution is that we do not have much control over the output formatting. We are stuck with whatever format returns, which is actually (and suprisingly) the result from tuple.__repr__. So the parenthesis that we get might be misleading, since we actually had a list, and not a tuple.
If this is too bad to put up with, I would recommend using the approach suggested by Zero Piraeus:
>>> a = [1, 2, 3]
>>> print(f'Unpacked list: {" ".join(str(i) for i in a)}')
This gives us the flexibility to format the list as we wish.
Answer from SenhorLucas on Stack Overflowjust thought id make a post incase anyone has an issue using f-string with a list. i moved on from using .format() to using f-string. i have found it pretty easy to get adjusted to but i was struggling with using specific values within a list, i treid a few methods like:
names = ['Adam', 'Bob', 'Cyril']
text = f'Winners are: {0} {1} {2}'(*names)
print(text)
names = ['Adam', 'Bob', 'Cyril']
text = f'Winners are: {*0} {*1} {*2}'(names)
print(text)
names = ['Adam', 'Bob', 'Cyril']
text = f'Winners are: {[0]} {[1]} {[2]}'(names)
print(text)
all came back with errors and left me stuck on how to use f-string with a list until i watched Corey Schafers video, within the half an hour i was pretty much trying to brute force it, reaching the same error, i found out the correct way of interlopoing a list with a f-string is:
names = ['Adam', 'Bob', 'Cyril']
text = f'Winners are: {names[0]}, {names[1]}, {names[2]}'
print(text)
just thought id make the post incase anyone else is unsure on how.
Shortest solution
Just add a comma after the unpacked list.
>>> a = [1, 2, 3]
>>> print(f"Unpacked list: {*a,}")
Unpacked list: (1, 2, 3)
There is a longer explanation to this syntax in this thread.
Caveat
With this solution is that we do not have much control over the output formatting. We are stuck with whatever format returns, which is actually (and suprisingly) the result from tuple.__repr__. So the parenthesis that we get might be misleading, since we actually had a list, and not a tuple.
If this is too bad to put up with, I would recommend using the approach suggested by Zero Piraeus:
>>> a = [1, 2, 3]
>>> print(f'Unpacked list: {" ".join(str(i) for i in a)}')
This gives us the flexibility to format the list as we wish.
Since any valid Python expression is allowed inside the braces in an f-string, you can simply use str.join() to produce the result you want:
>>> a = [1, 'a', 3, 'b']
>>> f'unpack a list: {" ".join(str(x) for x in a)}'
'unpack a list: 1 a 3 b'
You could of course also write a helper function, if your real-world use case makes the above more verbose than you'd like:
def unpack(s):
return " ".join(map(str, s)) # map(), just for kicks
>>> f'unpack a list: {unpack(a)}'
'unpack a list: 1 a 3 b'
Videos
roots = [sqrt(i) for i in numbers]
print(f'Roots: {roots}')So Im trying to gain control of the precision of my floats, as Im practicing list comprehension and such, this particular print doesn't work.
I really dig these new fstrings but I cant figure out where to put in the ':.3f in the code.
print(f'Roots: {[root for root in roots]}')This is as far as ive come but when i put in the formatter on root its 'invalid syntax'
Say I have a list [1, 2, 3, 4]
I want to be able to print with an f string that will output
1, 2, 3, 4
But if you use
Print(f"{*list,}")
You get
(1, 2, 3, 4)
I found a solution! Thanks for all your help.
print(f"The list is: {' '.join(str(x) for x in list)}")
Here is a way to do it using a list comprehension :
print([f"{x:.2}" for x in er])
Since, you wish to print the first 2 digits after the decimal of each individual elements. You will have to iterate over it & print them out as -
[f'{element:.2}' for element in er]
Usually we can apply aggregation type functions like len, sumdirectly on a list without needing to iterate over it.