Getting two python (List and Tuple type) [2-6 python list to replace elements]

2-6 python list replaced elements

Suppose now that the class is still three students:

1 >>> L = ['Adam', 'Lisa', 'Bart']

Now, Bart students to be transferred to another school, happened to be a classmate Paul, to update the list of members of the class, we can put Bart deleted, then Paul added.

Another way is to directly replace Paul to Bart:

1 >>> L[2] = 'Paul'
2 >>> print L
3 L = ['Adam', 'Lisa', 'Paul']

For one assignment list in the index, you can directly replace with new elements out of the original elements, the number of elements included in the list remain unchanged.

Since Bart -1 can also be used to index, therefore, the following code can accomplish the same replacement work:

1 >>> L[-1] = 'Paul'

 

Guess you like

Origin www.cnblogs.com/ucasljq/p/11585878.html