[Maya Python] Extending/Concatenating List Problem

Good day!

I am building an empty list by appending list from an old list. The code goes like this

    fkConChildren = fkChildren[0]
    fkConChildren += fkChildren[int((len(fkChildren) / 2))]
    fkConChildren += fkChildren[-1]

However, when I execute print (fkConChildren)
It results as

spine01_FK_conspine04_FK_conspine07_FK_con

I think what I need is something like this (with comma and quotation marks)

["spine01_FK_con","spine04_FK_con","spine07_FK_con"]

The reason is I get an error when I iterate over the list saying

# ValueError: No object matches name: i # 

Is there a way around this?

Thank you for looking at the problem

You should post the actual code you are trying to run if you want help with the error message.

But yes, when you += like that on a string, it adds more string to the string.

fkConChildren is not a list, because it is getting the first item of a list, so it is no longer a list. It is the first thing that was in the list. In this case, a string.

myList = ['test', 'foo', 'bar']

print( type(myList[0]) )
print( type(myList[0:1]) )

# Results in:
<type 'str'>
<type 'list'>

print(myList[0])
print(myList[0:1])

# Results in:
test
['test']

Then once you have a list, you can’t use += or + to add a string to a list. You can use list1 + list2 or you can list1.append(anotherString) or list1.extend(anotherList)

1 Like

Your assigning fkConChildren as the first index of the fkChildren list and not as a list itself containing the first child. To fix it assign it as a list:

fkConChildren = [fkChildren[0]]
fkConChildren.append(fkChildren[int((len(fkChildren) / 2))])
fkConChildren.append(fkChildren[-1])

Remember your appending a mutable list, not mutating an existing object.

1 Like

Thank you for the responses

@clesage
RE: post the actual code
Will keep that in mind in the next post. Thank you.

Thanks also for the referring the type() function. Seems like a good tool to troubleshoot

@chalk

Thanks for the code. It works as expected!