forked from bradtraversy/python_sandbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlists.py
More file actions
50 lines (35 loc) · 894 Bytes
/
lists.py
File metadata and controls
50 lines (35 loc) · 894 Bytes
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
# A List is a collection which is ordered and changeable. Allows duplicate members.
# Create a list
numbers = [1, 2, 3, 4, 5]
fruits = ['apples', 'oranges', 'grapes', 'pears']
# use a constructor
numbers2 = list((1, 2, 3, 4, 5))
print(numbers, numbers2)
# get a single value
print(fruits[1])
#length of array
print(len(fruits))
#append to the end
fruits.append('Mangoes'.lower())
print('append', fruits)
#remove maching item
fruits.remove('grapes')
print('remove', fruits)
#insert into a specific index
fruits.insert(2, 'strawberries')
print('insert', fruits)
#remove by position (pop method)
fruits.pop(2)
print('pop', fruits)
#reverse list
fruits.reverse()
print('reverse', fruits)
#sort (alphabetical)
fruits.sort()
print('sort', fruits)
#reverse Sort
fruits.sort(reverse = True)
print('reverse sort', fruits)
#change value
fruits[0] = 'monkeys'
print('change value', fruits)