0

I was recently studying someone's code and a portion of code given below

class Node:
def __init__(self, height=0, elem=None):
    self.elem = elem
    self.next = [None] * height

What does it mean by [None] * height in the above code

I know what does * operator (as multiplication and unpacking) and None means in python but this is somehow different.

Shaida Muhammad
  • 1,428
  • 14
  • 25
  • This simply generates a list which all the elements are None and the length of the list is height, when height equals to 0, it returns an empty list. – Jasper Zhou Nov 09 '19 at 07:47

3 Answers3

3

It means a list of Nones with a height number of elements. e.g., for height = 3, it is this list:

[None, None, None]
mshsayem
  • 17,557
  • 11
  • 61
  • 69
1

If you do -

[element] * 3

You get -

[element, element, element]

That's what the code does, [None] * height

That is, if -

height = 4
[None] * height 
# equals [None, None, None, None]
Sushant
  • 3,499
  • 3
  • 17
  • 34
0
>>> [None]  * 5
[None, None, None, None, None]

Gives you a list of size height in your case

bhaskarc
  • 9,269
  • 10
  • 65
  • 86