-
Notifications
You must be signed in to change notification settings - Fork 14
/
P16_ListSlicing
123 lines (113 loc) · 3.42 KB
/
P16_ListSlicing
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#! /bin/python
# Lists can be thought of the most general version of a sequence in Python. Unlike strings, they are mutable.
1.) Creating lists
2.) Indexing and Slicing Lists
3.) Basic List Methods
4.) Nesting Lists
# Assign a list to an variable named my_list
my_list = [1,2,3] #We just created a list of integers, but lists can actually hold different object types.
len(my_list) #Length of the list
3
Indexing and Slicing
my_list = ['one','two','three',4,5]
# Grab element at index 0
my_list[0]
'one'
# Grab index 1 and everything past it
my_list[1:]
['two', 'three', 4, 5]
# Grab everything UP TO index 3
my_list[:3]
['one', 'two', 'three']
We can also use + to concatenate lists, just like we did for strings.
my_list + ['new item']
['one', 'two', 'three', 4, 5, 'new item']
Note: This doesn't actually change the original list!
my_list
['one', 'two', 'three', 4, 5]
You would have to reassign the list to make the change permanent.
# Reassign
my_list = my_list + ['add new item permanently']
my_list
['one', 'two', 'three', 4, 5, 'add new item permanently']
We can also use the * for a duplication method similar to strings:
# Make the list double
my_list * 2
['one',
'two',
'three',
4,
5,
'add new item permanently',
'one',
'two',
'three',
4,
5,
'add new item permanently']
# Again doubling not permanent
my_list
['one', 'two', 'three', 4, 5]
#Basic List Methods
#Lists in Python, tend to be more flexible than arrays in other languages for a two good reasons:
1) They have no fixed size.
2) They have no fixed type constraint
# Create a new list
l = [1,2,3]
# Append
l.append('append me!')
# Show
l
[1, 2, 3, 'append me!']
Use pop to "pop off" an item from the list. By default pop takes off the last index, but you can also specify which index to pop off. Let's see an example:
# Pop off the 0 indexed it
p=l.pop(0)
# Show
p
2
# Assign the popped element, remember default popped index is -1
popped_item = l.pop()
popped_item
'append me!'
# Show remaining list
l
[3, 'append me!', 'append me!']
It should also be noted that lists indexing will return an error if there is no element at that index. For example:
l[100]
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-27-3e7ce3111e95> in <module>()
----> 1 l[100]
IndexError: list index out of range
We can use the sort method and the reverse methods to also effect your lists:
new_list = ['a','e','x','b','c']
#Show
new_list
['a', 'e', 'x', 'b', 'c']
# Use reverse to reverse order (this is permanent!)
new_list.reverse()
new_list
['c', 'b', 'x', 'e', 'a']
# Use sort to sort the list (in this case alphabetical order, but for numbers it will go ascending)
new_list.sort()
new_list
['a', 'b', 'c', 'e', 'x']
Nesting Lists
A great feature of of Python data structures is that they support nesting. This means we can have data structures within data structures. For example: A list inside a list.
# Let's make three lists
lst_1=[1,2,3]
lst_2=[4,5,6]
lst_3=[7,8,9]
# Make a list of lists to form a matrix
matrix = [lst_1,lst_2,lst_3]
# Show
matrix
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Now we can again use indexing to grab elements, but now there are two levels for the index. The items in the matrix object, and then the items inside that list!
# Grab first item in matrix object
matrix[0]
[1, 2, 3]
# Grab first item of the first item in the matrix object
matrix[0][0]
1