首页 > 代码库 > Python_week5_lists

Python_week5_lists

list

list.reverse()

list.sort()

list.index(obj):obj is the object to be find out; it returns index of the found object otherwise raise an exception indicating that value does not find.

aList = [123, ‘xyz‘, ‘zara‘, ‘abc‘];print "Index for xyz : ", aList.index( ‘xyz‘ ) ; //return  1print "Index for zara : ", aList.index( ‘zara‘ ) ;//return  2

list.append(x)

list.insert(i, x):insert x after list[i].

list.pop():the last item will be removed.

list.pop(3) :delete the list[3].the argument is index.so sometimes, you may use list.pop(list.index(obj))

list.remove(sth):delete the ‘sth‘.

list.extend(an_iter):add each item from the iterable an_iter onto the list.

Difference between extend and append:

 

 

iteration

warning: we can‘t modify a list while interating over it.

settle: create another list

Python_week5_lists