Basic Python help

Good morning
I’m resuming my python learning and well I have the following question…
that I am attending wrong.

Need an ‘:’ after the else I think.

2 Likes

Thank you so much

Hello
Datos =IN[0]
Cordially
Christian.stan

Sorry to bother…
Could you help me please?
I am trying to enter the list and sublists to be able to do a review but I get this image error.

How could I enter the list and sublists that I had…?? thank you so much…

Generally this would be a bad list structure, that you would typically avoid. To deal with it you would need to iterate over elements and also test them for their type to see if they are a list, but I’d suggest assessing why the data is of uneven structure first.

If you know you have either items or lists without more lists in them, something like this would work and preserve list structure:

# Uneven list structure
badList = [1,2,3,4,[5,6,7],8,9]

# Append to list
results = []

# Your function here
def evenOdd(e):
    if e % 2 == 0:
        return "even"
    else:
        return "odd"

# For each item in list
for i in badList:
    # If it is a list as well
    if isinstance(i, list):
        # Subresult to append to
        subResult = []
        # For each item in sublist, run function
        for j in i:
            subResult.append(evenOdd(j))
        results.append(subResult)
    # Otherwise, we are assuming it's an item
    else:
        results.append(evenOdd(i))

# Output
print(results)

1 Like