0% found this document useful (0 votes)
2 views13 pages

CDS M2

The document provides an overview of Python lists, including their creation, basic methods, and list slicing techniques. It outlines learning outcomes, demonstrates list declarations, indexing, and various list methods such as append, clear, and sort. Additionally, it includes examples to illustrate the usage of these methods and slicing operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views13 pages

CDS M2

The document provides an overview of Python lists, including their creation, basic methods, and list slicing techniques. It outlines learning outcomes, demonstrates list declarations, indexing, and various list methods such as append, clear, and sort. Additionally, it includes examples to illustrate the usage of these methods and slicing operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

List Functions

Prof. Ryan Paul Obligar


Learning outcomes:
- To simulate the basic structure of python list.
- To enumerate the methods under python list.
- To perform list slicing commands
- To apply basic list methods in some machine problems.

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 = []

#Empty list with initial size


myListSize = []*5

#List of integers
myListInt = [2, 5, 8]

#List of String
myListName = [“mark”, “jen”, “may”]

#List of mixed data types


myListMix = [7, “Hello”, 2.8]
4
Source: programiz.com
Displaying list contents and indexing
import os;
os.system('cls');

myList = ["alice", "bob", "charlie", "dylan", "eve", "frank"]


#displaying all lists contents
print ("Names: ", myList)
#displaying using index
print ("\nPlease step forward", myList[4])
print ("\nYou are fired!", myList[1])

5
List using negative index
import os;
os.system('cls');

myList = ["alice", "bob", "charlie", "dylan", "eve", "frank"]


#displaying all lists contents
print ("Names: ", myList)
#displaying using negative index
print ("\nPlease step forward", myList[-3])
print ("\nYou are fired!", myList[-6])

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

insert() Adds an element at the specified position


pop() Removes the element at the specified position

remove() Removes the first item with the specified value

reverse() Reverses the order of the list


sort() Sorts the list
Source: w3schools.com 9
List Methods
Method Syntax
append() list.append(elmnt)
clear() list.clear()
copy() list.copy()
count() list.count(value)

extend() list.extend(iterable)

index() list.index(elmnt)

insert() list.insert(pos, elmnt)


pop() list.pop(pos)

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']

names = ["ann", "bob", "cap", "bob", "dear"] points = [3, 6, 1, 8, 9, 2]


names.remove("bob") points.sort()
print (names) print(points)
Output: ['ann', 'cap', 'bob', 'dear'] Output: [1, 2, 3, 6, 8, 9]

fruits = ["apple", "banana", "cherry", "durian"]


fruits.reverse()
print(fruits)

Output:
['durian', 'cherry', 'banana', 'apple']
13

You might also like