Lists and For Loops list comprehension

Screen Link:

My Code:

row_1 = ['Facebook', 0.0, 'USD', 2974676, 3.5]
row_2 = ['Instagram', 0.0, 'USD', 2161558, 4.5]
row_3 = ['Clash of Clans', 0.0, 'USD', 2130805, 4.5]
row_4 = ['Temple Run', 0.0, 'USD', 1724546, 4.5]
row_5 = ['Pandora - Music & Radio', 0.0, 'USD', 1126879, 4.0]

app_data_set = [row_1, row_2, row_3, row_4, row_5]

[print(i) for i in app_data_set]

What I expected to happen:

['Facebook', 0.0, 'USD', 2974676, 3.5]
['Instagram', 0.0, 'USD', 2161558, 4.5]
['Clash of Clans', 0.0, 'USD', 2130805, 4.5]
['Temple Run', 0.0, 'USD', 1724546, 4.5]
['Pandora - Music & Radio', 0.0, 'USD', 1126879, 4.0]

What actually happened:

['Facebook', 0.0, 'USD', 2974676, 3.5]
['Instagram', 0.0, 'USD', 2161558, 4.5]
['Clash of Clans', 0.0, 'USD', 2130805, 4.5]
['Temple Run', 0.0, 'USD', 1724546, 4.5]
['Pandora - Music & Radio', 0.0, 'USD', 1126879, 4.0]
[None, None, None, None, None]
1 Like

This should solve it. Assigning to b.

b = [print(i) for i in app_data_set]

Hi @pabla007, welcome to the community!

A similar problem has been addressed here and hopefully, it helps you understand why the last line of output contains a list of Nones.

1 Like

@monorienaghogho Yes it worked for me.

1 Like

Yes i was wondering that why none is being returned thanks now my concept is cleared ^_^.

Good to know you have understood now.

Best!

Quite insighful… :+1:t5:

1 Like

Hello @monorienaghogho & @pabla007 although this will print the contents of the list, It is against the Zen of Python,

  • The print function returns None
  • List comprehensions always return a list

Rule 13 in the Zen of Python:

There should be one-- and preferably only one --obvious way to do it.

The best method to use here is to unpack the list in print and assign the sep argument \n:

>>> row_1 = ['Facebook', 0.0, 'USD', 2974676, 3.5]
>>> row_2 = ['Instagram', 0.0, 'USD', 2161558, 4.5]
>>> row_3 = ['Clash of Clans', 0.0, 'USD', 2130805, 4.5]
>>> row_4 = ['Temple Run', 0.0, 'USD', 1724546, 4.5]
>>> row_5 = ['Pandora - Music & Radio', 0.0, 'USD', 1126879, 4.0]
>>> app_data_set = [row_1, row_2, row_3, row_4, row_5]
>>> print(*app_data_set, sep="\n")
['Facebook', 0.0, 'USD', 2974676, 3.5]
['Instagram', 0.0, 'USD', 2161558, 4.5]
['Clash of Clans', 0.0, 'USD', 2130805, 4.5]
['Temple Run', 0.0, 'USD', 1724546, 4.5]
['Pandora - Music & Radio', 0.0, 'USD', 1126879, 4.0]
4 Likes

Unpacking is a nice method too.