Initializing List Members

I want to initialize a list in python. I do not understand why the code below does not work:

u = ['abc', 'de', 'fghi', 'jklm', 'n', ''] for item in u: item = len(item) 

There are other ways to initialize a list, for example:

 u = [len(item) for item in u] 

But, my question is why the first code does not work.

Edit: I am new to Python and am programming. Unfortunately, I do not understand some parts of your answers. For example:
- reformatting in reformatting the name of the item to the length of the name of the item "
- The iterator in the item item is a temporary variable pointing to the element u based on where the (implicit) iterator points. As far as I understand, my second example creates a new_list in memory and assigns the values ​​of new_list u . Regardless of previous values. But I want to know how I can change the values ​​in the first list. Thanks

+7
python list initialization
source share
6 answers

In the first case, you do not initialize anything. "item" is a temporary variable that points to an element u, based on where the (implicit) iterator points to. If you want to initialize the list in the first style, you can do it like:

 u = ['abc', 'de', 'fghi', 'jklm', 'n', ''] v = [] for item in u: v.append(len(item)) print v 
+4
source share

You retype the name item to the length of the item name, which was simply associated with the loop. Therefore, you cannot initialize list . All you do is reorder the variable at each iteration. Your understanding of a list is a perfectly acceptable method for initializing a list .

+3
source share

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]] 
+1
source share

The reason the first code doesn't work is because item is just a reference to every exterminated object in the sequence u . When you say item = len(item) , you really assign len(item) to item , but since item just acts as a pointer pointing to u[i] for step i * th * in your loop, it won’t actually change u[i] itself u[i] .

To change u[i] , you can either make a list view, as you showed in your question, or do something like:

 for i in range(len(u)): u[i] = len(u[i]) 

The third way is the very useful enumerate method introduced in python 2.3.

 for i, item in enumerate(u): u[i] = len(item) 

enumerate returns a list of tuples created from its argument (don't quote me on that ... but it basically acts like a list of tuples), where each tuple inside has the form (index, argument [index]).

+1
source share

The first code works. The u list is initialized in the first line, and the next two lines are executed without affecting the rest of the code - there is no point.

 u = ['abc', 'de', 'fghi', 'jklm', 'n', ''] 

Initializes the list u with the contents ['abc', 'de', 'fghi', 'jklm', 'n', ''] - all you need to do to initialize the list.

Lines

 for item in u: item = len(item) 

Just bind the name item to all values ​​in u immediately after overwriting item to the length property of the value one by one until the interpreter processes the entire list.

0
source share

item is just a name . in a for loop:

 for item in u: 

item is a reference to the object to which u [i] refers . In other words, item and u[i] refer to the same object.

but this statement:

 item = len(item) 

change the name item to a link to int whose value is len (item).

So nothing has changed in the list u.

List comprehension:

 u = [len(item) for item in u] 

just creates a new list, and the name u is now a link to the new list.

If you want to use for a loop, you can use the enumerate statement:

 for i, item in enumerate(u): u[i] = len(item) 

This changes the links of u elements one at a time .

0
source share

All Articles