Search code examples
pythonpython-3.xnonetypeiterable-unpacking

What does [None]* means in python


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.


Solution

  • 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]