Search code examples
pythonpseudocode

Pseudocode Translation to python with ":"


For the following pseudocode:

L1 = [(i, L[i]) : i < len(L)]

I'm struggling to interpret what it is doing and how to translate it to python, I've tried the following two ideas but i is referenced before assignment. Mostly I'm struggling to interpret the pseudocode although it should be clear.

if i < len(L):
        L1 = (i, L[I])

L1 = (i, L[i]) where(i < len(L))

Solution

  • Translate it to a list comprehension:

    L1 = [(i, L[i]) for i in range(len(L))]
    

    But Python has a built-in function that does this: enumerate():

    L1 = list(enumerate(L))