Lecture 9

Harold Nelson

2/15/2018

Lists and Strings

These are two similar types of python object.

A list is a sequential collection of python objects. A list is enclosed in square brackets and the individual objects are separated by commas. The individual objects (if any) may be any kind of object including a list.

A string is also a sequential collection of python objects, but a string is enclosed in quote marks and the individual objects are not separated at all. The individual objects (if any) are valid characters.

Both of these object types can be empty.

Both empty objects are useful in building more lengthy objects using an accumulator pattern.

Examples

astring = "abcd"
alist = ["a","b","c","d"]
print(astring)
print(alist)
## abcd
## ['a', 'b', 'c', 'd']

Both strings and lists respond to slicing in the same way.

astring = "abcd"
alist = ["a","b","c","d"]

partOfString = astring[1:3]
print(partOfString)

partOfList = alist[1:3]
print(partOfList)
## bc
## ['b', 'c']

Note that print() does not include quote marks when printing a string but does include square brackets when printing a list.

A HUGE Difference

Let’s look at the end of a string and a list. This is similar to what we did above. Also try to change the end of each

astring = "abcd"
alist = ["a","b","c","d"]

endOfString = astring[3]
print(endOfString)

endOfList = alist[3]
print(endOfList)

# Change the end of the list
alist[3] = 'x'
print(alist)

# astring[3] = 'x' fails.  Try in the idle shell
## d
## d
## ['a', 'b', 'c', 'x']

Lists can be changed. They are mutable. Strings Can’t be changed. They are immutable.

Operations on Lists

.

Figure 4.2 from the text

Figure 4.2 from the text

Examples

l1 = ["Tom",7.4,5,True]
l2 = [1.4,"Hi There!"]
l3 = l1 + l2
print(l3)
l4 = l1 * 2
print(l4)
l5 = [l1]*2
print(l5)
l6 = [l1,l1]
print(l6)
## ['Tom', 7.4, 5, True, 1.4, 'Hi There!']
## ['Tom', 7.4, 5, True, 'Tom', 7.4, 5, True]
## [['Tom', 7.4, 5, True], ['Tom', 7.4, 5, True]]
## [['Tom', 7.4, 5, True], ['Tom', 7.4, 5, True]]

Exercise

Create a list named l1 containing “Joe”,“Tom”,27.

Answer

l1 = ["Joe","Tom",27]

Exercise

Create a new list la with objects ‘a’,1 ,‘dog’. Create a new list lb with all of the objects from l1 and la.

Answer

l1 = ["Joe","Tom",27]
la = ['a',1,'dog']
lb = l1 + la
print(lb)
## ['Joe', 'Tom', 27, 'a', 1, 'dog']

Exercise

Using l1, create a list l2 containing two lists, each of which is a copy of l1.

Answer

l1 = ["Joe","Tom",27]
l2 = [l1] * 2
print(l2)

l2a = [l1,l1]
print(l2a)
## [['Joe', 'Tom', 27], ['Joe', 'Tom', 27]]
## [['Joe', 'Tom', 27], ['Joe', 'Tom', 27]]

Exercise

Using l1, create a list l3 containing 6 items, two copies of l1 in a single list.

Answer

l1 = ["Joe","Tom",27]
l3 = l1 * 2
print(l3)

l3 = l1 + l1
print(l3)
## ['Joe', 'Tom', 27, 'Joe', 'Tom', 27]
## ['Joe', 'Tom', 27, 'Joe', 'Tom', 27]

HUGE Change Part 2

An Example

l1 = ['a','b']
l2 = l1
l3 = ['a','b']
l4 = list(l1)
print(l1)
print(l2)
print(l3)
print(l4)

l1[1] = "x"
print(' ')
print("After the change")
print(" ")
print(l1)
print(l2)
print(l3)
print(l4)
## ['a', 'b']
## ['a', 'b']
## ['a', 'b']
## ['a', 'b']
##  
## After the change
##  
## ['a', 'x']
## ['a', 'x']
## ['a', 'b']
## ['a', 'b']

Are we building a new object or referring to the changed mutable object?

Python sees l2 as simply another name referring to the object associated with the name l1, but l3 and l4 are newly created objects.

Another Example

Consider the following python code.

l1 = ["Joe","Tom",27]
l3 = l1 * 2
print(l3)
l4 = [l1] * 2
print(l4)
l5 = [l1,l1]
print(l5)

l1[1] = "Sally"
print(' ')
print('After change to Sally')
print(' ')

print(l3)
print(l4)
print(l5)
## ['Joe', 'Tom', 27, 'Joe', 'Tom', 27]
## [['Joe', 'Tom', 27], ['Joe', 'Tom', 27]]
## [['Joe', 'Tom', 27], ['Joe', 'Tom', 27]]
##  
## After change to Sally
##  
## ['Joe', 'Tom', 27, 'Joe', 'Tom', 27]
## [['Joe', 'Sally', 27], ['Joe', 'Sally', 27]]
## [['Joe', 'Sally', 27], ['Joe', 'Sally', 27]]

List Methods

Here is a useful list of list methods from our textbook.

Table 4.2 from the textbook

Table 4.2 from the textbook

Note that list methods generally alter the list. String methods did not do this since strings are immutable.

Examples

alist = [1,4,"cat",3.14]
alist.append("dog")
print(alist)
## [1, 4, 'cat', 3.14, 'dog']
alist = [1,4,"cat",3.14]
alist.insert(1,"dog")
print(alist)
## [1, 'dog', 4, 'cat', 3.14]
alist = [1,4,"cat",3.14]
alist.pop()
print(alist)
## [1, 4, 'cat']
alist = [1,4,"cat",3.14]
alist.pop(1)
print(alist)
## [1, 'cat', 3.14]
# Note we can't sort with strings and numbers.
alist = [1,4,0,3.14]
alist.sort()
print(alist)
## [0, 1, 3.14, 4]
# Note we can't sort with strings and numbers.
alist = [1,4,0,3.14]
alist.reverse()
print(alist)
## [3.14, 0, 4, 1]
alist = ["a","b","c","b"]
bind = alist.index("b")
print(alist)
print(bind)
# Note that alist was not altered.
## ['a', 'b', 'c', 'b']
## 1
alist = ["a","b","c","b"]
bcnt = alist.count("b")
print(alist)
print(bcnt)
# Note that alist was not altered.
## ['a', 'b', 'c', 'b']
## 2
alist = ["a","b","c","b"]
alist.remove("b")
print(alist)
# Note that alist was altered.
## ['a', 'c', 'b']

Exercise

Create a list with 4 items: 1,“bird”,“cat”,“fish”.

Now add “snake” to the end of the list and print it.

Answer

alist = [1,"bird","cat","fish"]
alist.append("snake")
print(alist)
## [1, 'bird', 'cat', 'fish', 'snake']

Exercise

Whoops, I wanted snake to be ahead of cat but after bird. Please fix this and print the revised list.

Answer

alist = [1,"bird","cat","fish"]
alist.append("snake")
alist.pop()
alist.insert(2,"snake")
print(alist)
## [1, 'bird', 'snake', 'cat', 'fish']

Exercise

Create a function which removes all occurrences of an item from a list. The function must have two arguments, a list and an item. Test it by removing both ’b’s from the list [“a”,“b”,“c”,“b”]

Answer

def removeAll(lst,item):
    cnt = lst.count(item)
    for i in range(cnt):
        lst.remove(item)
    return lst    
alist = ["a","b","c","b"]
blist = removeAll(alist,"b")
print(blist)
## ['a', 'c']