Lecture
3 The Lists
Definition: A list is an ordered set of values, where each value is
identified by
an index. The values that make up a list are called its elements.
LISTs are in brackets!!!!!!!!!!!
1.
List values
[10,
20, 30, 40]
["spam", "bungee", "swallow"]
The
first example is a list of four integers. The second is a list of three
strings. The elements of a list don’t have to be the same type. The
following list contains a string, a float, an integer, and another list:
["hello",
2.0, 5, [10, 20]]
A list within
another list is said to be nested.
2.
The range()
function
range(start,
stop[, step]). the bracket doesn't mean you need a bracket there, it
indicates the contents in the brackte is optional.
Lists that
contain consecutive integers are common, so Python provides a simple
way to create them:
The range()
function can create the list but cannot display it to you. You must use
the list() function to display them (force them into the LIST format).
Also, if you
only type range(stop), the list will start with 0:
Define the
steps:
Deal with
negative numbers:
If there is only
a negative number in the range() function, you will get nothing since
the default starting number is 0.
3. Mixed
data types
4.
Accessing
elements
In Matlab, we
use parentheses for the index when
accessing the elements in a vector. Here in Python, we use brackets instead.
Negative index
Arithmetic
index:
5.
List length
int variable has
no length:
I didn't run it
but len(listie[0]) will be 5. The length of "spam!" is 5.
Really? Why this
works:
But this doesn't
work:
The reason is
the len() function only works for LISTs (also tuples and dictionaries which will be introduced later) and strings but not a real
integer.
You can force an int data into a string data type using the str() function.
One more thing,
the
length of a string equals to the number of letters/symbols in the
string:
6.
List
membership:
7.
List
operations: (Be carefule, the numbers inside the list won't participate in the math operation, this is not the 'matrix operation').
8.
List slices:
9.
Lists are
mutable:
Replace multiple
elements by another one:
Remove multiple
elements from the list:
And we can add
elements to a list by squeezing them into an empty slice at the desired
location:
10.
List
deletion:
del removes an
element from a list:
11.
Nested lists
12.
Matrices:
For example:
We
can use the nested list to represent a 2D matrix. But this is not the
only way to do this in Python. We'll learn 'array' in the
next few
lectures.
To access the
matrix:
This
is still a 1D list, but we just assume it to be a matrix. The more
often used matrix in Python is from the numpy function package, which
is called numpy.array. We will cover this in the later lectures.
13. The methods for the list() function.
list.append():
Try this code:
To append multiple values to the list, using .extend() instead of .append(). It is explained below in the '.extend()' section.
list.insert():
.extend:
How do you append multiple numbers to a list?
Becareful that you can't use .append() to do it, you should use extend instead.
list.sort() and list.reverse():
Sort,
reverse. Lists maintain the order of their elements. And they can be
reordered. With the sort method, we change the order of elements from
low to high. And: With reverse, we invert the current order of the
elements. Sort and reverse can be combined (for a reversed sort).
list.remove():
The
list.remove() function only removed the first 'Bill' but not the
second. So it is not ideal for removing some specific contents from a
list. Instead, we will use loops to remove something from a list in the
future.
Tasks:
1. Are they lists? (Answer these questions using comment lines in Spyder)
a. [2.0, 5, [10, 20], "hello"]
b. [1, 2, 3, [1, 2, 3, [1, 2, 3]]]
c. ["Hi"]
d. "Hi"
e. 1, 2, 3, 4, 5
f. (1, 2, 3, 4, 5)
g. {1, 2, 3, 4, 5}
2. Use the range() function to create the following number series in a list: -1, 0, 1, 2, 3, 4, 5, 6
3. Given a list list_1 = ["apple", [1, 2, 3, 4], 1, [1, 2]]:
a. Extract the entire "apple" string.
b. From list_1, extract '2' in the sublist of [1, 2, 3, 4]
c. From list_1, extract the entire [1, 2] sublist
d.
What is the returned value of list_1[0], list_1(0), list_1[1][1],
list_1[-1], and list_1[-2]? If you are not sure, try these in Spyder.
4.
Insert the list ["1", "2"] after "apple" in the list of
["apple", [1, 2, 3, 4], 1, [1, 2]]. (Please be careful here, this
is asking you to insert the entire list to there but not two single
strings to there). (use two different methods)
5. How to remove 'b', and 'c' from the list: listie=['a', 'b', 'c', 'd', 'e']. (use three different methods)
6. Combine list aa=[6,7,8,9] and list bb=[3,2,34] together to create list cc. (use two different methods)
7. How can you print out the length of an integer '1267886'.
8. Sort the numbers in list=[54,45,67,89,100] from low to high and then from high to low. Print the results for both.
9.
How do you append 1,2,3, the three int data, to the end of
listie=[4,5,6]. How do you append [1,2,3], the list, to the end of
listiee=[7,8,9]?