Over the week I been working with lists, learning more about indexing, slicing and virtually how to access each character in a list and manipulate it.
## BREAKDOWN OF THE WEEK
* **list:** a list is a collection of items or data in a particular oder enclosed by a square bracket [] and seperated with a comma. Eg
cars = ["Benz", "BMW", "Ferrari", "Lamborghini"]
fruits = ["apple","mango", "orange", "strawberry"]
numbers = [1,2,3,4,5,6,7,8,9,10]
* **indexing:** method of accessing each element or item from a list. eg
cars = ["Benz", "BMW", "Ferrari", "Lamborghini"]
1. to access element from the list
print(cars[0]) *output: Benz (accesses the element at index 0*
print(numbers[4]) *output: 5 (acesses element at index 4*
**slicing:** slicing is used to extract a subset or range of elements or items from a sequence.
[start:stop:step]. Eg
message = "Hello, World!"
print(message[0:5]) output: "Hello" (*element from index 0-5)*
* **appending:** to add an element or item to a list, mostly at the end of the list. Eg. to add avocado to the list below.
fruits = ["apple","mango", "orange", "strawberry" ]
new_fruit = fruits.append("avocado")
fruits = ["apple","mango", "orange", "strawberry", "avocado"] *(avocado was added to the list)*
*** **insering:** method used to insert and element, value or item to a list, usually using the postion to be added. eg, to insert banana to mango postion.
fruits = ["apple","mango","orange","strawberry","avocado"]
new_fruit = fruits.insert(1, banana)
fruits = ["apple","banana","mango","orange","strawberry","avocado"] *(banana has been added to index 1 in the list*)
*** **sorting:**** sorting simply means the arrangement of element in the list in alphabetical order, ABCD or 1234.
fruits = ["apple","avocado","banana"mango","orange","strawberry"]
**Remove, delete or pop an element in a list:** to remove, delete or pop an item from a list, use the command below:
1. del(fruits[0]) ==> to delete first item in the list fruits ==> "apple" *will be deleted *or
2. fruits.remove("apple") ==> "apple" *will be removed*
3. fruit_name = fruits.pop(0) ==> "apple" *will be popped out, using the index[0]*
**list length:** to obtain the total length of a list, the total number of elements or items in a list, use the command below.
print(len(fruits)) ==> *total number of items will displayed.*
fruits = ["apple","mango","orange","strawberry","avocado"]
length = 5 *(total number of items in the list is 5)*
**finding elements index:** each item or element in a list can be indentified by it position which is the index, use the command below to assess their index.
fruit_name = fruits.index("orange") ==> 2
**using boolean:** using the boolean returns True if the item is in the list otherwise it returns False if not find.
fruits = ["apple","mango","orange","strawberry","avocado"]
1. print("mango" in fruits) ==> *True (because mango is in the list fruits)*
2. print("kiwi" in fruits) ==> *False (because kiwi is not in the list fruits)*
**reversing a list:** this simply means making first element or items first and last first. eg
fruits = ["apple","mango","orange","strawberry","avocado"]
fruit_list = fruits.sort(reverse = True)
fruits = ["avocado","strawberry","orange","mango","apple"]
**Accessing Maximum & Minimum elements in a list**
this is the method of finding the biggest and smallest item mostly natural numbers or float in a list.
1. to find max number
number = [20,10,50,30,9]
print(max(number)) ==> to find biggest number in the list which is 50
2. to find min number
number = [20,10,50,30,9]
print(min(number)) ==> *the min number will be 9.*
@Blockfuse Lab, different methods will be given to you, with vivid explanations and examples follow by series of tasks to test your comprehension and lastly a timer hands_on fridays to round up your weekly assimilation. love the energy and the enabling envinronment... More articles every week.