Python Lists
Posted:
Thursday, June 14th, 2012Last modified:
Sunday, March 29th, 2015The basics of working Python lists.
list1 = ['apple', 1] list2 = ['banana', 2] lists = [list1, list2] print lists # [ ['apple', 1], ['banana', 2] ] print lists[0] # ['apple', 1] print lists[0][1] # 1 # Autofilling a list list3 = 3 * ['cherry'] + ['danish'] print list3 # ['cherry', 'cherry', 'cherry', 'danish'] # Slicing numbers = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16] print numbers[3:11] # [3, 4, 5, 6, 7, 8, 9, 10] Note the first value is inclusive. print numbers[9:] # [9, 10, 11, 12, 13, 14, 15, 16] print numbers[:6] # [0, 1, 2, 3, 4, 5] print numbers[12:20] # [12, 13, 14, 15, 16] print numbers[-4:] # [13, 14, 15, 16] # Note again, the first index is inclusive. print numbers[8:-3] # [8, 9, 10, 11, 12, 13] print numbers[-7:-3] # [10, 11, 12, 13] print numbers[:] # Prints the entire sequence. print numbers[1:11:2] # [1, 3, 5, 7, 9] Prints slice, incrementing by steps of two print numbers[::3] # [0, 3, 6, 9, 12, 15] # Concatenating sequences list4 = list1 + list2 + list3 print list4 # ['apple', 1, 'banana', 2, 'cherry', 'cherry', 'cherry', 'danish'] # A list sequence cannot be concatenated with a string sequence # list5 - list4 + "I am a string" # This would throw the error below. ''' list5 - list4 + "I am a string" NameError: name 'list5' is not defined ''' # Multiplying sequence list6 = [None] * 5 print list6 # [None, None, None, None, None] # Print the number of items in a sequence. print len(numbers) # 17 # Print the smallest value. print min(numbers) # 0 print max(numbers) # 16 print min(lists) # ['apple', 1] # Checking for membership print 'apple' in list4 # True print 'Scandanavia' in list4 # False # Check how many times an element appears in a sequence print list3.count('cherry') # 3 # Get the index of an item print list3.index('danish') # 3 print list3[3] # danish aString = 'This is a string' print 'i' in aString # True print 'is' in aString # True in version 2.3+. Will throw an exception # in earlier versions. # In works recursively as well print 'apple' in list4 # True # Convert a string to a list aList = list(aString) print aList # ['T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 's', 't', 'r', 'i', 'n', 'g'] # Join a list into a string bString = ''.join(aList) print bString # This is a string # Updating lists bList = ['a', 'b', 'c'] bList[2] = 3 print bList # ['a', 'b', 3] del bList[1] print bList # ['a', 3] numbersB = numbers[:] numbersB[10:] = ['a', 'b', 'c'] print numbersB # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'b', 'c' numbersB[5:10] = [] print numbersB # [0, 1, 2, 3, 4, 'a', 'b', 'c'] numbersB[5:5] = [5, 6, 7] print numbersB # [0, 1, 2, 3, 4, 5, 6, 7, 'a', 'b', 'c'] numbersB = numbers[:] del numbersB[::3] print numbersB # [1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16] numbersB.append('appended') print numbersB # [1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 'appended'] ext = ['a', 'b', 'c'] numbersB.extend(ext) print numbersB # [1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 'appended', 'a', 'b', 'c'] numbersB = numbers[:] del numbersB[5] numbersB.insert(5,"five") print numbersB # [0, 1, 2, 3, 4, 'five', 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] print numbersB.pop() # 16 print numbersB.pop(10) # 10 print numbersB # [0, 1, 2, 3, 4, 'five', 6, 7, 8, 9, 11, 12, 13, 14, 15] # (10 and 16 are gone) # Remove the first occurence print list3 # ['cherry', 'cherry', 'cherry', 'danish'] list3.remove('cherry') print list3 # ['cherry', 'cherry', 'danish'] list3.reverse() print list3 # ['danish', 'cherry', 'cherry'] moreNumbers = [126, 8, 9832, 54] print sorted(moreNumbers) # [8, 54, 126, 9832] print moreNumbers # Original sequence order is unchanged. moreNumbers.sort() print moreNumbers # Original sequence has been altered. anotherList = ['run', 'whirlwind', 'sing', 'Alpha', 'aardvark', 9] anotherList.sort() print anotherList # [9, 'Alpha', 'aardvark', 'run', 'sing', 'whirlwind'] anotherList.sort(reverse=True) # Reverse order print anotherList # ['whirlwind', 'sing', 'run', 'aardvark', 'Alpha', 9] # Remove the 9 because our next sort will use the len() function. Integers don't # have a length, so this would cause an error if we left it in place. anotherList.remove(9) anotherList.sort(key=len) # Key can refer to any function print anotherList # ['run', 'sing', 'Alpha', 'aardvark', 'whirlwind']
!!Categories
[[!python]], [[!data structures]]
Available Wiki Topics
The operator of this site makes no claims, promises, or guarantees of the accuracy, completeness, originality, uniqueness, or even general adequacy of the contents herein and expressly disclaims liability for errors and omissions in the contents of this website.