[1,2,3] len(arrayName) # size of list # add value to all elements of list my_list=[1,3,5,7,8] new_list = [x+1 for x in my_list] # [2,4,6,8,9] list.append(obj) list.extend(list) list.index(obj) list.count(obj) # count appearance of obj # array to string and string to array sentence = ["there", "is", "no", "spoon"] ' '.join(sentence) #'there is no spoon' list=[3.1,121.2,34.1] ', '.join(map(str,list)) #'3.1, 121.2, 34.1' '3.1, 121.2, 34.1'.split(', ') #['3.1','121.2','34.1'] # 2nd number make how many split point '3.1, 121.2, 34.1'.split(', ',1) #['3.1','121.2, 34.1'] # list sort (modify itself) versionList.sort(reverse=1) # higher to lower versionList.sort() str([-4, -2, 0, 2, 4]) # '[-4,-2,0,2,4]' bb_str ='_'.join(map(str, bb)) # -4_-2_0_2_4 # list compare [-4, -2, 0, 2, 4] == [-4, -2, 0, 2, 3] # false # multiple list operation wMap_zero_Jnt=[x for x in wMap_All_Jnt if x not in wMap_Jnt_tgt] # remove list from list a = [1, 2, 3, 4, 5] b = [9, 8, 7, 6, 5] common = set(a) & set(b) #[5], no order applied [i for i, j in zip(a, b) if i == j] # [5] for equal-sized lists, which order matters version_list_prefix=["previous","current"] version_list_postfix=["A","B"] version_list_color=["rgb(255, 128, 128)","rgb(85, 170, 255)"] for each,post,color in zip(version_list_prefix,version_list_postfix,version_list_color): print(each+" "+post+" is "+color) # list slice operation # n = len(L) # item = L[index] # seq = L[start:stop] # seq = L[start:stop:step] seq = L[::2] # get every other item, starting with the first seq = L[1::2] # get every other item, starting with the second seq=L[::-1] # reverse reading list list(reversed(seq)) # generate a list reversed L.reverse() # reverse list, it changed original list seq=L[::-2] # reverse reading list, and get every other item list=[1,2,3,4,5,6] seq=list[::-2] # [6,4,2] # list to pair list info = ['name','John', 'age', '17', 'address', 'abc'] info_pair = zip(info[::2], info[1::2]) # [('name', 'John'), ('age', '17'), ('address', 'abc')] # list flatten, and remove duplicates nestList = [[2,3],[1,2,4],[5,6]] flatList = set(sum(nestList,[])) # for small list of 2 level nest # delete a element from list a = [1,2,3,4] del(a[2]) # [1,2,4] # for large list import itertools flatList = set( list(itertools.chain.from_iterable(nestList)) ) # for same type element list, no mix of list and element like [[5],6], as it will error import operator flatList = set( reduce(operator.add, map(list, nestList)) ) # like itertool, but it will not error for mix of list and element situation # for any mix list def flatFn(mainList): newList = [] for each in mainList: if isinstance(each, (list, tuple)): newList.extend( flatFn(each) ) else: newList.append(each) return newList flatList = set( flatFn(nestList) )