You cannot change the contents of a list simply by assigning a new value to the name that represents the entry in the list. You need to use the index assignment, for example, to change the first entry in the list to 'foo' , you could do u[0] = 'foo' .
In this case, you can do something like the following (although understanding the list from your question is cleaner):
for i, item in enumerate(u): u[i] = len(item)
Please note that if the items in your list are mutable, you can directly change the item, you simply cannot reassign them. For example, with a list of lists:
u = [['a'], ['b', 'c']] for item in u: item.append(len(item)) # u is now [['a', 1], ['b', 'c', 2]]
Andrew Clark
source share