CDS M2
CDS M2
2
Python Lists
“In Python programming, a list is created
by placing all the items (elements) inside
square brackets [ ] , separated by
commas.”
Source: programiz.com
Source: pybloggers.com
3
Basic Python list declaration
#Empty list
myList = []
#List of integers
myListInt = [2, 5, 8]
#List of String
myListName = [“mark”, “jen”, “may”]
5
List using negative index
import os;
os.system('cls');
Source: railsware.com
6
Slicing a list
myList = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
print ("Output1: ", myList[1:6])
print ("Output2: ", myList[0:4])
print ("Output3: ", myList[1:8:2]) To access a range of items in a list, you
print ("Output4: ", myList[0:9:3]) need to slice a list. One way to do this is
print ("Output5: ", myList[8:4:-1]) to use the simple slicing operator :
print ("Output6: ", myList[9:1:-2])
print ("Output7: ", myList[-6:-2])
print ("Output8: ", myList[7:3]) With this operator you can specify where
print ("Output9: ", myList[-2:-5]) to start the slicing, where to end and
print ("Output10: ", myList[-2:-5:-1]) specify the step.
print ("Output11: ", myList[:6])
print ("Output12: ", myList[3:])
print ("Output13: ", myList[:7:2]) myList=[start:stop:step]
print ("Output14: ", myList[4::2])
print ("Output15: ", myList[:]) start = start index position
print ("Output16: ", myList[::]) stop = ending index position
print ("Output17: ", myList[::3])
print ("Output18: ", myList[::-1]) Step = increment (optional)
print ("Output19: ", myList[::-2])
print ("Output20: ", myList[4::-1])
7
Slicing a list
#------------0----1----2----3----4----5----6---7----8---9 positive indexes
myList = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
#----------10----9----8----7----6----5----4---3----2---1 negative indexes
print ("Output1: ", myList[1:6])
print ("Output2: ", myList[0:4])
print ("Output3: ", myList[1:8:2])
print ("Output4: ", myList[0:9:3])
print ("Output5: ", myList[8:4:-1])
print ("Output6: ", myList[9:1:-2])
print ("Output7: ", myList[-6:-2])
print ("Output8: ", myList[7:3])
print ("Output9: ", myList[-2:-5])
print ("Output10: ", myList[-2:-5:-1])
print ("Output11: ", myList[:6])
print ("Output12: ", myList[3:])
print ("Output13: ", myList[:7:2])
print ("Output14: ", myList[4::2])
print ("Output15: ", myList[:])
print ("Output16: ", myList[::])
print ("Output17: ", myList[::3])
print ("Output18: ", myList[::-1])
print ("Output19: ", myList[::-2])
8
print ("Output20: ", myList[4::-1])
List Methods
Method Description
append() Adds an element at the end of the list
clear() Removes all the elements from the list
copy() Returns a copy of the list
count() Returns the number of elements with the specified value
extend() Add the elements of a list (or any iterable), to the end of the current list
index() Returns the index of the first element with the specified value
extend() list.extend(iterable)
index() list.index(elmnt)
remove() list.remove(elmnt)
reverse() list.reverse()
sort() list.sort()
Source: w3schools.com 10
List method examples:
fruits = ["apple", "banana", "cherry"] fruits = ["apple", "banana","cherry", "durian"]
fruits.append("durian") x = fruits.copy()
print (fruits) print (fruits)
print (x)
Output:
['apple', 'banana', 'cherry', 'durian'] Output:
['apple', 'banana', 'cherry', 'durian']
['apple', 'banana', 'cherry', 'durian']
a = ["apple", "banana“, "cherry"]
b = ["durian", "egg", "flower"]
a.append(b) fruits = ["apple", "banana","cherry", "durian"]
print (a) fruits.clear()
print (fruits)
Output:
['apple', 'banana', 'cherry', ['durian', 'egg', 'flower']] Output:
[]
11
fruits = ["apple", "banana", "cherry", "durian"]
List method examples: fruits.insert(2, "berry")
print(fruits)
Output:
points = [2, 3, 4, 5, 6, 7, 3, 4, 3, 1, 3] ['apple', 'banana', 'berry', 'cherry', 'durian']
x = points.count(3)
print(x) fruits = ["apple", "banana", "cherry", "durian"]
fruits.pop(1)
Output: print(fruits)
4 Output: ['apple', 'cherry', 'durian']
points = ["a", "b", "c", "d", "e", "f", "g"] names = ["bryan", "romel", "carl", "mark"]
x = points.index("c") names.pop(2)
print(x) print (names)
Output: ['bryan', 'romel', 'mark']
Output:
2 insect = ["ant", "bee", "fly", "spider"]
insect.pop()
print(insect)
Output: ['ant', 'bee', 'fly'] 12
List method examples:
fruits = ["apple", "banana", "cherry", "durian"] names = ["julia", "sadie", "michelle", "anna", "rita"]
fruits.remove(cherry) names.sort()
print(fruits) print (names)
Output: ['apple', 'banana', 'durian'] Output: ['anna', 'julia', 'michelle', 'rita', 'sadie']
Output:
['durian', 'cherry', 'banana', 'apple']
13