-1

my code is....

names = ['ali', 'parham', 'hasan', 'farhad']
print('names: ', [print(i) for i in names])

and my output is this...

ali
parham
hasan
farhad
names:  [None, None, None, None]

but i want this....

names: ali, parham, hasan, farhad

why what are in the list is 'None'?!

and why the name are in the above lines?!

  • Because `print` returns `None`. – Axe319 Dec 06 '21 at 18:52
  • Change your second line to `print('names: ', [i for i in names])` – Ilia Dec 06 '21 at 18:53
  • 5
    @Ilia isn't `[i for i in names]` just a weird way to write `names`? Why should they change their second line to include such a pointless thing? – John Coleman Dec 06 '21 at 18:55
  • 4
    @Ilia `[i for i in names]` is the same as `names`, no? Just use `print('names:', names)` – jarmod Dec 06 '21 at 18:55
  • "and why the name are in the above lines?!" to evaluate `[print(i) for i in names]`, python needs to first evaluate the function calls `print(i)` and those function calls of course print what they are asked to print. – John Coleman Dec 06 '21 at 18:56
  • Does this answer your question? [Why does the print function return None?](https://stackoverflow.com/questions/27959258/why-does-the-print-function-return-none) – Axe319 Dec 06 '21 at 18:56
  • @JohnColeman , @jarmod yes, of course, you are both correct. The list comprehension is useless in this case. `print('names': names)` is sufficient. – Ilia Dec 06 '21 at 18:56
  • In Python 3, `print` is a function. Every function returns some value once it finishes, and if it doesn't, it return `None` by default. You can just use `print("names: ", names)` to get the output you want. – demberto Dec 06 '21 at 18:57
  • 1
    It isn't clear just what output you want, `print('names:', ', '.join(names))` is my guess. – John Coleman Dec 06 '21 at 18:58
  • @lia its print ```names: ['ali', 'parham', 'hasan', 'farhad']``` i want something like ```names: ali, parham, hasan, farhad``` – PaRHaM_PwP Dec 06 '21 at 19:09

1 Answers1

1

When you do print('names: ', [print(i) for i in names]) the following happens: To do the first print Python has to evaluate the arguments of the function. The second argument is [print(i) for i in names]. Here you loop over the names, print them and put the result of the call to print in the list. This result is always None.

So after this first step all the names are printed and then you're virtually left with print('names: ', [None, None, None, None]). Now print will output exactly that.

If you want to combine the entries of a list to a string use the strings join method. Here are some examples from the interactive interpreter.

>>> '-'.join(['A', 'B', 'C'])
'A-B-C'
>>> 'x'.join(['A', 'B', 'C'])
'AxBxC'
>>> ' '.join(['A', 'B', 'C'])
'A B C'
>>> values = ['x', 'yy', 'zzz']
>>> ' + '.join(values)
'x + yy + zzz'
>>> names = ['ali', 'parham', 'hasan', 'farhad']
>>> ', '.join(names)
'ali, parham, hasan, farhad'

So what you need here is:

print('names:', ', '.join(names))

or with an f-string

print(f"names: {', '.join(names)}")

This will give you names: ali, parham, hasan, farhad.

Matthias
  • 12,873
  • 6
  • 42
  • 48