Lists, tuples and immutablility AND Lambda functions

Introduction

SORT

list slice:

v[1:3]

+ can also do third thing?

reverses???

v[::-1]

Introduction

Python get array size, size of other iterables, sets

map and filters run on iterable. also the loops? is a set an iterable?

+ conditional substitutions of lists in python

Lists

Python lists are dynamic arrays.

a = [1,2,3]

Lists are mutable.

How to do each of following:

concatenation

slice/filter

insert

pop

traverse

map

sort

reverse

List slices

List comprehensions

Eg the following defines a list, and then the second line returns that list.

my_list = [1,2,3]
[x for x in my_list]

can do functions too. following returns [3,4,5]

[x + 2 for x in my_list]

filter

[x for x in my_list if x >2]

can be on any iterable:

[x for x in range(10)]

Lambda functions

filter

+ map function + lambda functions

Tuples

a = (1,2,3)

Tuples are immutable.

why not just use copy on write instead of tuples? + complex to implement?

why tuples broadly safety + mean that if create one tuple based on another, deep copy? if mutable + a=(1,2,3) + b=a + b[0]=2 * this fails, but if it didn’t we might get the following + print(a) * (2,2,3) + print(b) * (2,2,3)

with mutability can still do eg a=a[2] because this is creating a new thing and using it in name immutability means can’t do eg b[3] = 2 allows optimiser to assume not mutable. can cause speed ups