5b: Sequences II
Table of Contents
1. Looking back: 5a
- Indexing strings
- Sequences: strings, ranges, tuples, lists.
- Tuples
- Lists
2. Methods for lists
A method is similar to a function (but not the same).
A method is invoked by using the dot notation; be aware that some methods might require arguments, and some other do not. Check the available documentation when in doubt.
When you create an object of a certain Data Type, you can then apply any of the methods available for that Data Type.
Below you can find some useful methods for Lists
2.1. append
.append(<value>) Adds <value> to the (end of the) list
sentence = ["This Monday UvA staff will strike", 42, True, [1, 2, 3]] sentence.append("world") print(sentence)
2.2. index
.index(<value>). Returns the index number of the first element with <value>.
sentence = ["This Monday UvA staff will strike", 42, True, [1, 2, 3]] print(sentence.index(42))
You can also make Python start looking for the first element with that value at another index:
test_list = [1,1,5,5,7,1,9] print(test_list.index(1,3))
2.3. fruitful and void methods
Some methods - like .index - return a value, others - like .append - simply do something.
test_list = [1,1,5,5,7,1,9] a = test_list.append(10) # a mistake that I sometimes make print(a)
2.4. insert
.insert(<index>, <value>). Insert <value> to list before position <index>
> What do you think? Fruitful or void?
test_list = [1,1,5,5,7,1,9] test_list.insert(2,"hi") print(test_list)
2.5. sort
.sort(<value>). Sort all values in the list in place (replacing the list itself), by default in ascending order.
> Fruitful or void?
test_list = [100,1,5,5,7,1,9] test_list.sort() print(test_list)
By default .sort sorts in ascending order. But you can add an argument to make it sort in descending order.
test_list = [100,1,5,5,7,1,9] test_list.sort(reverse=True) print(test_list)
strings = ["a", "bb","baaa","c","d"] strings.sort() print(strings)
2.6. reverse
.reverse(). Reverses the order of the list elements, in place, that is, replacing the list itself
numbers = ["a", "b","c","d"] numbers.reverse() print(numbers)
2.7. count
.count(<value>). Returns the number of ocurrence of <value> in the list
> Fruitful or void?
numbers = [1, 3, 5, 2, 4, 3, 6, 7] print(numbers.count(3))
2.8. pop
.pop(<index>). Remove and returns the element with <index> from list. If no <index> is provided, then it will remove the last one.
> Fruitful or void?
numbers = [1, 3, 5, 2, 4, 3, 6, 7] a = numbers.pop(1) print(a)
2.9. copy
Two variables can refer to the same list:
sentence = ["This Monday UvA staff will strike", 42, True, [1, 2, 3]] copy_list = sentence copy_list.append(1987) print(copy_list) print(sentence)
If you are not sure if a variable refers to the same list (or any other object), you can use the function id.
sentence = ["This Monday UvA staff will strike", 42, True, [1, 2, 3]] copy_list = sentence print(id(copy_list)) print(id(sentence)) print("--------") sentence2 = ["This Monday UvA staff will strike", 42, True, [1, 2, 3]] copy_list2 = ["This Monday UvA staff will strike", 42, True, [1, 2, 3]] print(id(copy_list2)) print(id(sentence2)) print("--------") sentence3 = ["This Monday UvA staff will strike", 42, True, [1, 2, 3]] copy_list3 = sentence3.copy() print(id(copy_list3)) print(id(sentence3))
If you want to make an independent copy, you can either simply repeat the assignment statement, or use the .copy method.
sentence = ["This Monday UvA staff will strike", 42, True, [1, 2, 3]] copy_list = sentence.copy() sentence.append(1987) print(copy_list) print(sentence)
sentence = ["This Monday UvA staff will strike", 42, True, [1, 2, 3]] copy_list = ["This Monday UvA staff will strike", 42, True, [1, 2, 3]] sentence.append(1987) print(copy_list) print(sentence)
3. Creating a list from scratch
lst = [1,2,4]
The most common technique when you want to create a new list from scratch (and for example, with certain conditions) would be to start with an empty list, and then in combination with a for-loop and the method .append() build the rest.
Let's start with this problem:
> Create a list with all the integers from 10 up to and including 20
lst = [] for i in range(10,21): lst.append(i) print(lst)
> create a list with all the odd numbers from 10 up to and including 20
lst = [] for i in range(11,21,2): lst.append(i) print(lst)
lst = [] for i in range(10,21): if i % 2 != 0: lst.append(i) print(lst)
> A very common problem is to "clean lists". Filter certain Data Types.
input_list = [1, 2, True, "hi", 1.5] lst = [] for i in input_list: #if isinstance(i, int): (apparently isinstance treats True/False *also* as int (not just as bool) if type(i) == int: lst.append(i) print(lst)
print(isinstance(True,int))
> Extra: why does this not work?
data = [13.0, 78.9, "a", "b", 87, 90, "c", 89, "z"] for i in range(len(data)): if type(data[i]) != str: data.pop(i) print(data)