0
ts_file = "ts_nfl_04_red_zone_conversion"

ts_title = [print(i + ' ', end="") for i in ts_file[7:].upper().split("_")]

Result:

04 RED ZONE CONVERSION [None, None, None, None]

What produce this list of None statement and how I can avoid it? Many thanks.

yatu
  • 86,083
  • 12
  • 84
  • 139
p-a
  • 77
  • 9
  • 2
    `print` doesn't return anything, so what did you expect? List comprehensions are from building new lists out of old ones, if you just want to print everything in the list, then use a normal loop. – Robin Zigmond Feb 06 '19 at 08:46
  • 1
    Possible duplicate of [Understanding Python 3 lists printing None value for each element](https://stackoverflow.com/questions/17707002/understanding-python-3-lists-printing-none-value-for-each-element) – Sayse Feb 06 '19 at 08:47
  • Saving it to a variable as you are ostensibly doing will not print the list. – TigerhawkT3 Feb 06 '19 at 08:48

6 Answers6

1

You could instead join the elements resulting from the splitting the string:

ts_title = (' ').join(ts_file[7:].upper().split("_"))
# '04 RED ZONE CONVERSION'
yatu
  • 86,083
  • 12
  • 84
  • 139
1

You can use replace instead:

>>> ts_file[7:].upper().replace("_", " ")
a_guest
  • 34,165
  • 12
  • 64
  • 118
0

List comprhension isn't really necessary here as a print statement returns a None type. You can just loop through the string like this:

for i in ts_file[7:].upper().split("_"): print(i + " ", end="")
Tom Dee
  • 2,516
  • 4
  • 17
  • 25
0

As mentioned in the comments, print returns a None and is not intended be used in a list comprehension.

if you want a one-liner to achieve your desired result, you can do something like

print(' '.join(ts_file[7:].upper().split("_")))
pixie999
  • 468
  • 5
  • 11
0

You produced a list of None because you created one. That's what a list comprehension is for: creating lists. However, expressions aren't automatically printed unless you're in the interactive interpreter and have returned the expression to the top level, which means you wouldn't actually see this output given that you're saving it to a variable.

Anyway, you're supposed to use a regular loop in this situation, as follows:

ts_file = "ts_nfl_04_red_zone_conversion"

for i in ts_file[7:].upper().split("_"):
    print(i + ' ', end="")

However, you can of course still use a comprehension and create an empty list by rearranging it slightly, saving it to a variable so that it isn't automatically printed in the interactive interpreter:

ts_file = "ts_nfl_04_red_zone_conversion"

ts_title = [None for i in ts_file[7:].upper().split("_") if print(i + ' ', end="")]
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
0

As mentioned by others, print() does not return anything. Hence, None is printed. If you are wondering why the elements are properly printed, and then followed by 4 None's, its because of how functions work.

A function is called and once every statement inside has been executed, a value is returned, but only if the function returns something.

In your case, print(i + ...) called the print function on i, print was executed, meaning that it printed i to the console, and then its value was returned, which is None, since print() does n't return anything.

Coming to the solution, you could use the .join() method or the replace() method:

a = ts_file[7:].upper().replace("_", " ")
print(a)

or

a = (' ').join(ts_file[7:].upper().split("_"))
print(a)

The output:

04 RED ZONE CONVERSION

You could also do another thing, if you did n't care about what was stored in ts_title: As soon as you assign ts_title with your list comprehension:

ts_title = [print(i + ' ', end="") for i in ts_file[7:].upper().split("_")]

if you run your script, you will get the expected output on the screen, just as I had explained at the start of the answer.

Polaris000
  • 938
  • 1
  • 11
  • 24