Learning to Use Lists
1. Creating and Accessing Lists
Lists will always use [].
1.1 Creating Lists
1.2 Accessing List Elements
Remember, index 0 is always the first, -1 means it will always choose whatever is last on the list.
The example below is accessing the items in the list but then displaying the message with whatever list is chosen in the index.
2. Modifying Lists
Lists are muteable, this means you can change them after it has been created.
2.1 Adding Elements to a List
There are two ways to add items into an existing list. The first method with adding another router called ‘Router2’, ‘append()’ will add the item into the end of the list. If you wanted to add the item at the start or which ever position on the list you can use the .’insert()’ keyword. the ‘0’ in the insert() is telling the code to add the ip ‘4.4.4.4’ at the start of the list.
2.2 Removing Elements from a List
The below example allows you to remove the item on the list by typing the exact match.
An alternative to remove the item on the list by using indexing you can use the pop() keyword.
2.3 Modifying Elements in a List
The below example allows you to change the item on the list referring to the index value.
3. Iterating Through Lists
You may need to do something with a list which goes through different iterations in a loop, for example you may need to check a list of IP addresses in a list. So using a loop will save you time.
3.1 Using a for
Loop to Iterate Through a List
The example below, I ave created a list which contains a list of devices. I then create a ‘for’ loop called ‘device’ and the ‘device ‘for’ loop will call in the list ‘devices’. I then display the list of devices.
3.2 Using enumerate()
to Get Index and Value
‘enumerate()’ allows you to get the index positioning of the list as well as the list of items in that list.
The example below we have a list created called ‘ip_addresses’ and contains IPs.
We then create a ‘for’ loop called ‘index’ (this could be called anything) and we create the enumerate called ‘ip’ (this could also be called anything) but it is used to call the ‘enumerate()’ function.
For each iteration it prints the device number and then the IP address. The device number is used for every iteration to add 1 to it. In this example it is index +1 to begin with on the first iteration, as index begins with 0 it will be 0+1 = 1 and it will print out the first item on the list of ‘IP_addreses’, after the first iteration it will be 1+1 = 2 and so on.
4. Slicing Lists
Slicing allows you to create a new list from an existing list.
4.1 Basic Slicing Syntax
Basic slicing you use ‘list[start:stop]’, so you would call the existing with the index.
4.2 Omitting Start or Stop
You don’t have to enter the start:stop and just either enter the start or just the stop.
The below example illustrates different ways to slice the first 3 items and also slicing the last two items.
5. List Comprehensions
5.1 Basic List Comprehension
The example below, we created ‘uppercase’ where we called the action upper() and called it device.upper(), the ‘for device in devices’ is then used as a loop and iterates over each item in the list ‘devices’ and changes it to Capitals.
5.2 Filtering with List Comprehensions
The example below can be broken down into parts for easy understanding:
device.startswith(“Rout”)] – Is to include anything that starts with ‘Rout’ in the list called ‘devices’
‘for device in devices” – Is a loop that iterates over each ‘device’ in the ‘devices’ list.
‘device for device in devices if is a list comprehension that filters and creates a new list. It will only include devices that have the word “Rout”.
What is highlighted in BLUE below can be called anything.
The below example starting with ‘lowercase’ in Yellow is changing the list of ‘devices’ to lower case.
We create another comprehension called ‘router_devices’ and ‘device’ in pink is doing something with the item. the ‘devices’ after the for in Orange is what you will be looping over again in the for loop.
6. Sorting and Reversing Lists
6.1 Sorting Lists
You can sort lists in ascending order.
Or you can sort out in descending order. ‘sorted’ is a built in function in Python. it is used to return a new, sorted list. In essence it will provide a new list based on what you have requested.