-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlists.py
139 lines (97 loc) · 4.55 KB
/
lists.py
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#What is a collection of items in a particular order can be of any type letters, numbers, strings, etc.
#Square brackets [] indicate a list, and individual elements in the list are separated by commas.
#Syntax: list_name = [item1, item2, item3, ...]
grocery_list = ["Milk", "Eggs", "Bread", "Butter"]
print(grocery_list)
#Accessing Elements in a List
#You can access items in the list by their index.
#index starts at 0, not 1.
grocery_list = ["Milk", "Eggs", "Bread", "Butter"]
print(grocery_list[0])
#You can also use string methods in a list.
east_africa_countries = ["kenya", "uganda", "TANZANIA", "rwanda"]
print(east_africa_countries[0].title())
print(east_africa_countries[1].upper())
print(east_africa_countries[2].lower())
#Using Individual Values from a List - building a sentence/message.
east_africa_countries = ["kenya", "uganda", "TANZANIA", "rwanda"]
message = f"I world like to visit {east_africa_countries[3].title()} someday"
print(message)
#Changing, Adding, and Removing Elements
#The list are dynamic , meaning you can add,remove and change.
#changing an element in a list
grocery_list[0] = "Rice" #changing he value of first index
print(grocery_list)
grocery_list[3] = "Peanut Butter" #changing the value of last index
print(grocery_list)
#Adding an element to a list
#using append() method - adds new list item to the end.
#append() method takes one argument, the item to add to the list.
grocery_list.append("Groundnuts")
print(grocery_list)
#adding items to an empty list
trip_destinations = []
trip_destinations.append("Maldives")
trip_destinations.append("San Francisco")
trip_destinations.append("New York")
print(trip_destinations)
#inserting elements into a list at any position
#using insert() method - adds new list item at any position.
#insert() method takes two arguments, the item to add to the list and the index position.
todo = ["Do laundry", "Clean room", "Wash dishes"]
todo.insert(0, "Wake up early") #will insert at the beginning of the list
print(todo)
#Removing an element from a list
#using del statement - but you must know the index position of the item you want to remove. You can no longer access the value that was removed.
#del statement deletes an item at a specific index position.
todo = ["Do laundry", "Clean room", "Wash dishes"]
del todo[0] #will delete the first item in the list
print(todo)
#using pop() method to remove an item from a list
#pop() method removes an item from the end of the list and stores it in a variable so you can use it after removing it.
todo = ["Do laundry", "Clean room", "Wash dishes"]
last_todo = todo.pop() #will remove the last item in the list
print(last_todo)
print(todo)
#use it to print statement
print(f"The last to do was {last_todo} and it was popped")
#using idex position to pop an item from a list
first_todo = todo.pop(0)
print(first_todo)
print(f"The first todo was {first_todo} it was popped using index position")
#Each time you use po() the item is no longer stores in the list
#using remove() method to remove an item from a list if you only know the item and not the index position.
#remove() only removes the first occurrence of the value you specify.
todo = ["Do laundry", "Clean room", "Wash dishes"]
todo.remove("Wash dishes")
print(todo)
todo = ["Do laundry", "Clean room", "Wash dishes"]
done_todo = "Do laundry" #assigning the value to a variable
todo.remove(done_todo)
print(f"The {done_todo} task is completed")
#Organizing a List
#using sort() method to sort a list permanently
cars = ["bmw", "audi", "toyota", "subaru"]
cars.sort()
print(cars)
#sort() method sorts the list alphabetically in ascending order permanently.
#sort() method is case sensitive in alphabetical order.
#we can sort the list in reverse alphabetical order by passing the argument reverse=True to the sort() method.
cars = ["bmw", "audi", "toyota", "subaru"]
cars.sort(reverse=True)
print(cars)
#using sorted() function to sort a list temporarily without altering the original list
cars = ["bmw", "audi", "toyota", "subaru"]
print(f"Here is the original cars list:\n {cars}")
sorted_cars = sorted(cars)
print(f"\nHere is the sorted list: \n{sorted_cars}")
print(f"\nHere is the original cars list again which is not affected {cars}")
#using reverse() method to reverse the original order of a list.
cars = ["bmw", "audi", "toyota", "subaru"]
cars.reverse()
print(f"Here is the reverse order : {cars}")
#using len() function to find the length of a list
#len() counts the numbers of items starting from 1
cars = ["bmw", "audi", "toyota", "subaru"]
cars_length = len(cars)
print(f"Here is the length of the list :{cars_length}")