home
clear breadcrumbs
search
login
 
list
import os os.system('clear') lst = ['a','b','c'] for item in lst: print(item) # a # b # c print(lst) # ['a', 'b', 'c'] # 0 1 2 3 # index range (including lower bound, but not including upper bound of range) print(lst[0:3]) # ['a', 'b', 'c'] print(lst[:3]) # ['a', 'b', 'c'] print(lst[2:]) # ['c', 'd'] last = lst.pop() print(last) # d print(lst) # ['a', 'b', 'c'] print(lst[-1]) # c del lst[1] print(lst) # ['a', 'c', 'd'] # index range print(lst[1:3]) # appending to the end of the list lst.append('b') lst.append('c') print(lst) # ['a', 'b', 'c'] # insert(index, value) lst.insert(0,'A') print(lst) # ['A', 'a', 'b', 'c'] # list_1.extend(list_2) append contents of list_2 to list_1 lst_2 = ['d','e','f'] lst.extend(lst_2) print(lst) # ['A', 'a', 'b', 'c', 'd', 'e', 'f'] lst.remove('A') print(lst) # [a', 'b', 'c', 'd', 'e', 'f'] lst.pop() print(lst) # ['a', 'b', 'c', 'd', 'e'] import os # os.system('clear') lst = ['a','b','c'] for item in lst: print(item) # a # b # c print(lst) # ['a', 'b', 'c'] # 0 1 2 3 # index range (including lower bound, but not including upper bound of range) print(lst[0:3]) # ['a', 'b', 'c'] print(lst[:3]) # ['a', 'b', 'c'] print(lst[2:]) # ['c', 'd'] last = lst.pop() print(last) # d print(lst) # ['a', 'b', 'c'] print(lst[-1]) # c del lst[1] print(lst) # ['a', 'c', 'd'] # index range print(lst[1:3]) # appending to the end of the list lst.append('b') lst.append('c') print(lst) # ['a', 'b', 'c'] # insert(index, value) lst.insert(0,'A') print(lst) # ['A', 'a', 'b', 'c'] # list_1.extend(list_2) append contents of list_2 to list_1 lst_2 = ['d','e','f'] lst.extend(lst_2) print(lst) # ['A', 'a', 'b', 'c', 'd', 'e', 'f'] lst.remove('A') print(lst) # [a', 'b', 'c', 'd', 'e', 'f'] lst.pop() print(lst) # ['a', 'b', 'c', 'd', 'e'] lst.reverse() print(lst) # ['e', 'd', 'c', 'b', 'a'] print(lst) # ['a', 'b', 'c', 'd', 'e'] lst_3 = [7,3,9,1] lst_3.sort() print(lst_3) # [1, 3, 7, 9] lst_3.sort(reverse=True) print(lst_3) # [9, 7, 3, 1] lst_4 = sorted(lst_3) print('original list' + str(lst_3)) # original list[9, 7, 3, 1] print('new list sorted' + str(lst_4)) # new list sorted[1, 3, 7, 9]