List 1
List 1
There are four collection data types in the Python programming language: (list, tuple, set, dictionary)
List
Lists are used to store multiple items in a single variable. Lists are one of 4 built-in data types in Python used to store
collections of data, the other 3 are tuple, set, and dictionary, all with different qualities and usage. Lists are created
using square brackets:
List items
List items are indexed, the first item has index [0], the second item has index [1] etc.
List length: to determine how many items a list has, use the len() function:
Print the number of items in the list:
list_A = ["apple", "banana", "cherry"]
print(len(list_A))
Output: 3
List Items - Data Types: List items can be of any data type, or can contain different data types (string, int and
boolean).
list1 = ["apple", "banana", "cherry"] #The data types of list elements are string
list2 = [1, 5, 7, 9, 3] #The data types of list elements are string
list3 = [True, False, False] #The data types of list elements are string
list4 = ["abc", 34, True, 40, "male"] #A list with strings, integers and boolean values:
Access items: list items are indexed and you can access them by referring to the index number. The first item has
index 0, and the last item has index (length -1)
Example: print the second item of the list.
print(listB[0])
Output: 100
print(listB[5])
Output: 70
1
print(listB
Output: [100, 50, 300, 800, 40, 70]
Negative indexing: negative indexing means start from the end. -1 refers to the last item, -2 refers to the second last
item etc.
Range of indexes: you can specify a range of indexes by specifying where to start and where to end the range.
When specifying a range, the return value will be a new list with the specified items.
Example: return the third, fourth, and fifth item:
Note: the search will start at index 2 (included) and end at index 5 (not included).
Remember that the first item has index 0.
By leaving out the start value, the range will start at the first item: This example returns the items from the beginning
to the fourth item.
By leaving out the end value, the range will go on to the end of the list. This example returns the items from 300 to
the end:
Range of negative indexes: specify negative indexes if you want to start the search from the end of the list:
This example returns the items from 300 (-4) to, but NOT including 70 (-1):
Change item value: to change the value of a specific item, refer to the index number:
Example: change the second item:
2
Output: [100, 707, 300]
Change a range of item values: to change the value of items within a specific range, define a list with the new
values, and refer to the range of index numbers where you want to insert the new values:
Example: change the values 50 and 300 with the values 700 and 520.
If you insert more items than you replace, the new items will be inserted where you specified, and the remaining
items will move accordingly.
Example: change the second value by replacing it with two new values:
Note: The length of the list will change when the number of items inserted does not match the number of items
replaced.
If you insert fewer items than you replace, the new items will be inserted where you specified, and the remaining
items will move accordingly:
Example: change the second and third value by replacing it with one value:
Loop Lists
Loop through a list: you can loop through the list items by using a for or while loop:
Example: print all items in the list, one by one.
Output: Apple---Banana---Orange---
Example: print the squares of all items in the list, one by one.
3
listN = [1, 5, 7,8,10]
for x in listN:
print(x*x, end=" -")
Loop through the index numbers: you can also loop through the list items by referring to their index number.
Use the range() and len() functions to create a suitable iterable.
Example: print all items by referring to their index number:
listA = ["Apple, "Banana", "Orange"]
for i in range(len(listA)):
print(listA[i])
Output:
Apple
Banana
Orange
Output: 100,50,300,800,40,
Use the len() function to determine the length of the list, then start at 0 and loop your way through the list items by
referring to their indexes. Remember to increase the index by 1 after each iteration in while loop.
Example: print all items using a while loop to go through all the index numbers
Output: 80 3 20
List methods
append(): adds an element at the end of the list
clear(): removes all the elements from the list
copy(): returns a copy of the list
count(): returns the number of elements with the specified value
extend(): add the elements of a list (or any iterable), to the end of the current list
index(): returns the index of the first element with the specified value
insert(): adds an element at the specified position
pop(): removes the element at the specified position
remove(): removes the item with the specified value
reverse(): reverses the order of the list
sort(): sorts the list on has a set of built-in methods that you can use on lists.
Append Items: To add an item to the end of the list, use the append() method.
4
Example: using the append() method to append an item:
listA = ["apple", "banana", "cherry"]
listA.append("orange")
print(listA)
Insert Items: to insert a list item at a specified index, use the insert() method.
Note: As a result of the examples above, the lists will now contain 4 items.
Extend List: To append elements from another list to the current list, use the extend() method.
Example: add the elements of listB to listA:
Output: [10,20,30,100,500,1000]
Remove specified item: the remove() method removes the specified item.
Remove specified index: the pop() method removes the specified index.
Example: remove the second item:
listA = ["apple", "banana", "cherry"]
listA.pop(1)
print(listA)
If you do not specify the index, the pop() method removes the last item.
5
Example: Remove the last item:
listA = ["apple", "banana", "cherry"]
listA.pop()
print(listA)
Exercises