Tuples
Definition
An ordered sequence of elements, can mix elements types
- dentoed by ( )
te = () # an empty tuple
te = (2,"one",3)
in: te[0]
out: 2
(2,"one",3)+(5,6)
(2,"one",3,5,6)
in: te[1:2]
out: ("one",)
# with a comma, indicates that this is a tuple insted of expression of single element
Charateristic:
- immutable
te[1] = 4 # ---> this is an error
- conveniently to swap variable values
(x,y) = (y,x)
- return more than 1 value from a function
return (r,p)
- can interate over tuples
def get_data(aTuple):
nums = ()
words = ()
for t in aTuple: # This expression is worktable
nums = nums +(t[0],)
if t[1] not in words:
words = words + (t[1],) # HERE!!!! (t[1],) ,this comma is extremely important
min_nums = min(nums)
max_nums = max(muns)
unique_words = len(words)
return (min_nums, max_nums, unique_words)
Exercise
x = (1, 2, (3, 'John', 4), 'Hi')
# x is a tuple
# while for the sigle element in the tuple...
In x[0]
Out 1
In type(x[0])
Out int
# nested tuple
In x[2]
Out (3, 'John', 4)
In type(x[2])
Out tuple
In x[2][2]
Out 4
# for the single element in tuple...
In x[-1][-1]
Out 'i'
# the length of a tuple
In len(x)
Out 4
Lists
definition
Ordered sequence of information, accessiable by index
x = [1,2,3,'Hi']
characteristic
- mutable
L = [2,1,3]
In: L[1] = 5
# now L is [2,5,3]
- index can be variable or expression
L[i-1] #expression
- interation over the list
L = [1,2,3]
for i in L:
print(L[i])
Exercise
x = [1, 2, [3, 'John', 4], 'Hi']
# Tho here is noly one element, still treated as a list
In x[0:1]
Out [1]
List operations
5. Data Structures - Python 3.10.2 documentation
- ADD
method | append(x) | extend(interable) | insert(index,item) |
---|---|---|---|
return | None | None | None |
- Deletion
method | remove(x) | pop ([i]) | del(L[i]) | clear |
---|---|---|---|---|
return | None | popped item | None | None |
note | remove the first item == x, Error is no such thing | remove all |
- Sorted
method | L.sort | L.sort() | sorted(L) |
---|---|---|---|
return | function | None | sorted item |
note | mutates the list, returns nothing | does not mutate the list, must assign to something |
Mutation, Aliasing, Cloning
Aliasing
warm = ['orange','yellow','pink']
hot = warm # this two are aliased
hot[0] = 'red'
In: warm
Out: ['red','yellow','pink']
# pointing to the same object
Cloning
cool = ['blue','green','grey']
chill = cool[:]
chill.extend('black')
# changes in chill won't change cool
# they point to different objects
Mutation
- Sorting list
Click here
List of list of list
warm = ['yellow','orange']
hot = ['red']
brightcolors = [warm]
brightcolors.append(hot)
In: print(brightcolors)
Out: [['yellow','orange'],['red']]
def remove_dups(l1,l2):
for e in l1:
if e in l2:
l1.remove(e)
In: l1 = [1,2,3,4]
l2 = [1,2,4,5]
remove_dups(l1,l2)
Out: [2,3,4]
💡 WHY 2 is not removed?
because Python uses an internal counter to keep track of index it is in the loop.
mutation changes the list length but Python doesn’t update the counter.
def remove_dups(l1,l2):
l1_copy = l1[:]
for e in l1_copy:
if e in l2:
l1.remove(e)
In: l1 = [1,2,3,4]
l2 = [1,2,4,5]
remove_dups(l1,l2)
Out: [2,3,4]
Functions as objects,dictitionaries
def applyToEach(L,f):
for i in range(len(L)):
L[i] = f(L[i])
In: applyToEach(L,abs)
In: applyToEach(L,int)
In: applyToEach(L,fact)
def applyFuns(L,x):
for f in L:
print(f(x))
In: applyFuns([abs,int,fact,fib],4)
-
MAP
simple form a unary function and collection of suitable arguements
for elt in map(abs,[1,-2,3,-4]) print(elt) [1,2,3,4]
general form - an n-ary function and n collections of arguments
L1 = [1,28,36] L2 = [2,57,9] for elt in map(min,L1,L2) print(elt) # compare each elements in the N place of L1&L2 [1,28,9]
Review
Type Type of elements Example of literals Mutable str characters ‘’,’a’,’abc’ No tuple any type ( ),(3,),(’abc’,4) No range integers range(10),range(1,10,2) No list any type [1],[3],[’abc’,4] Yes