-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLists.py
More file actions
55 lines (40 loc) · 924 Bytes
/
Lists.py
File metadata and controls
55 lines (40 loc) · 924 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
50
51
52
53
54
55
[1, 3, 400, 7]
# => [1, 3, 400, 7]
#creating an empty list automatically
list()
# => []
list_abc = ['a', 'b', 'c']
list_abc[0]
# => 'a'
list_abc[1]
# => 'b'
list_abc[2]
# => 'c'
list_abc[0] = 'z' #replaces 'a' with 'z'
list_abc
# => ['z', 'b', 'c']
list_abc.append('d')
list_abc
# => ['z', 'b', 'c', 'd']
len([1, 3, 400, 7])
# => 4
sorted([5, 100, 234, 7, 2])
# => [2, 5, 7, 100, 234]
sorted([5, 100, 234, 7, 2], reverse=True)
# => [234, 100, 7, 5, 2]
sorted([5, 100, 234, 7, 2], key=lambda x: x % 10) #sorts by last digit of each number ( lambda is an anonymous function)
# => [5, 7, 2, 100, 234]
list_123 = [1, 2, 3]
list_123.append(4)
print(list_123)
# => [1, 2, 3, 4]
# Check if an item exists in a list
'z' in ['a', 'b', 'c']
# => False
'z' in ['a', 'b', 'c', 'z']
# => True
# insert item at the last index
list_abc = ['a', 'b', 'c']
list_abc.insert(len(list_abc), 'd')
list_abc
# => ['a', 'b', 'c', 'd']