The concept of range of indexes is used to access a subset of elements from a list, tuple, or string.
aka slicing
Syntax: sequence[start:stop:step]
start is the index where the slice starts (inclusive)
stop is the index where the slice ends (exclusive)
step is the interval between each index for slicing
can use negative indexes to slice from the end
e.g., list[-3:] starts at the 3rd from last to the end of list
Searching a List
The in keyword is used to check if a specified item is present in a list, tuple, string, or other iterable object.
returns True or False
ip_addresses = ["192.168.1.1", "192.168.1.2", "192.168.1.3", "192.168.1.4", "192.168.1.5"]search_ip = "192.168.1.3"# Check if the IP address is in the listip_present = search_ip in ip_addressesprint(ip_present) # Output: True
Return Index of Item in a List
use the index("item") method on a list
list = ["this", "is", "a", "list"]myIndex = list.index("is")# should get 1
Modifying Lists
Changing a List Item
The value of a specific item in a list can be changed by referring to the index of the item
Syntax is list[index] = new_value
Change a Range of Items in a List
Syntax is list[start:stop] = new_values, where new_values is a list of new values
Inserting an Item into a List
The insert() method is used to add an item at a specific position in a list.
Syntax is list.insert(index, element)
where index is the position in the list where the new element should be inserted
Add List Items
The append() method is used to add an item to the end of a list.
Syntax: list.append(element)
element is the item to be added
Extend List
The extend() method is used to add multiple items to the end of a list.
- Syntax: list.extend(iterable)
- iterable can be a list, tuple, string, or any iterable object
basically appending multiple items
Removing List Items
The remove() method is used to remove the first occurrence of a specified item from a list.
Syntax: list.remove(element)
element is the item to be removed
Remove a Specified Index
The pop() method is used to remove an item at a specified position in a list and return it.
- Syntax: list.pop(index)
can store the item in a variable for later use since it is returned
del Keyword
The del keyword is used to delete objects.
for lists, can be used to remove an item at a specific position, or to delete a slice of a list
Syntax:
del list[index]
del list[start:stop]
Clear a List
The clear() method is used to remove all items from a list.
Syntax: list.clear()
Sorting Lists
The sort() method is used to sort the items in a list in ascending order.
Syntax: list.sort()
optional key parameter can be provided to specify a function of one argument that is used to extract a comparison key from each element
modifies the list it is called on
does not work on lists that contain both string and numeric data
only works on lists
Example
ip_addresses = ["192.168.1.3", "192.168.1.1", "192.168.1.2"]# Sort the IP addressesip_addresses.sort(key=lambda ip: tuple(map(int, ip.split('.'))))print(ip_addresses) # Output: ['192.168.1.1', '192.168.1.2', '192.168.1.3']
IP addresses are sorted in ascending order
key parameter is used with a lambda function that splits each IP address at the ‘.’ and converts each part to an integer
ensures that the IP addresses are sorted numerically rather than lexicographically
To keep the original list in tact, use the sorted() function instead
Sort Descending
The reverse=True keyword argument is used with the sort() method or the sorted() function to sort the items in a list in descending order.
Case-Insensitive Sorts
A case-insensitive sort can be performed by using the sort() method or the sorted() function with a key parameter.
key parameter is set to a function that converts each item to lowercase before comparison