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
40 lines (27 loc) · 631 Bytes
/
lists.py
File metadata and controls
40 lines (27 loc) · 631 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
# A List is a collection which is ordered and changeable. Allows duplicate members.
# Create list
numbers = [1, 2, 3, 4, 5]
fruits = ['Apples', 'Oranges', 'Grapes', 'Pears']
# Use a constructor
# numbers2 = list((1, 2, 3, 4, 5))
# Get a value
print(fruits[1])
# Get length
print(len(fruits))
# Append to list
fruits.append('Mangos')
# Remove from list
fruits.remove('Grapes')
# Insert into position
fruits.insert(2, 'Strawberries')
# Change value
fruits[0] = 'Blueberries'
# Remove with pop
fruits.pop(2)
# Reverse list
fruits.reverse()
# Sort list
fruits.sort()
# Reverse sort
fruits.sort(reverse=True)
print(fruits)