• 6 December 2024

Python Lists, Dictionaries, Tuples & Sets

Extending a list or concatenation of lists in other words. Extend function adds the given list at the end of the first list. Also, we can use insert function to add an item to a specific index of a list.

>>> my_list =["a","b","c"]
>>> my_list.extend(["d","e"])
>>> my_list
['a', 'b', 'c', 'd', 'e']
>>> my_list.extend(["x","y","z"])
>>> my_list.insert(5,"f")
>>> my_list
['a', 'b', 'c', 'd', 'e', 'f', 'x', 'y', 'z']

List pop, append, remove, clear & sorted usage.. When we use pop method of a list without an argument, it returns the last item of the list and removes it. If we pass the index of a specific item to the pop method, it returns and removes the item whose index is given. Also we can pass the item as an argument to remove, so it finds and removes it.

>>> vlans = [ f"vlan_{x}" for x in range(1,6)]
>>> vlans
['vlan_1', 'vlan_2', 'vlan_3', 'vlan_4', 'vlan_5']
>>> vlans.pop()
'vlan_5'
>>> vlans.pop()
'vlan_4'
>>> vlans.pop()
'vlan_3'
>>> vlans
['vlan_1', 'vlan_2']
>>> vlans.append("vlan_3")
>>> vlans.append("vlan_4")
>>> vlans.append("vlan_5")
>>> vlans.append("vlan_6")
>>> vlans.append("vlan_7")
>>> vlans
['vlan_1', 'vlan_2', 'vlan_3', 'vlan_4', 'vlan_5', 'vlan_6', 'vlan_7']
>>> vlans.pop(1)
'vlan_2'
>>> vlans.pop(5)
'vlan_7'
>>> vlans
['vlan_1', 'vlan_3', 'vlan_4', 'vlan_5', 'vlan_6']
>>> vlans.remove("vlan_1")
>>> vlans
['vlan_3', 'vlan_4', 'vlan_5', 'vlan_6']
>>> vlans.clear()
>>> vlans
[]

>>> ifs = [f"ge-0/0/{x}" for x in range(0,23)]
>>> ifs
['ge-0/0/0', 'ge-0/0/1', 'ge-0/0/2', 'ge-0/0/3', 'ge-0/0/4', 'ge-0/0/5', 'ge-0/0/6', 'ge-0/0/7', 'ge-0/0/8', 'ge-0/0/9', 'ge-0/0/10', 'ge-0/0/11', 'ge-0/0/12', 'ge-0/0/13', 'ge-0/0/14', 'ge-0/0/15', 'ge-0/0/16', 'ge-0/0/17', 'ge-0/0/18', 'ge-0/0/19', 'ge-0/0/20', 'ge-0/0/21', 'ge-0/0/22']
>>> sorted_ifs = sorted(ifs)
>>> sorted_ifs
['ge-0/0/0', 'ge-0/0/1', 'ge-0/0/10', 'ge-0/0/11', 'ge-0/0/12', 'ge-0/0/13', 'ge-0/0/14', 'ge-0/0/15', 'ge-0/0/16', 'ge-0/0/17', 'ge-0/0/18', 'ge-0/0/19', 'ge-0/0/2', 'ge-0/0/20', 'ge-0/0/21', 'ge-0/0/22', 'ge-0/0/3', 'ge-0/0/4', 'ge-0/0/5', 'ge-0/0/6', 'ge-0/0/7', 'ge-0/0/8', 'ge-0/0/9']

Dictionary keys , values, items

>>> device
{'hostname': 'r1', 'username': 'usr1234', 'password': 'abc123'}

>>> type(device.keys())
<class 'dict_keys'>
>>> type(device.values())
<class 'dict_values'>
>>> type(device.items())
<class 'dict_items'>
>>> keys = list(device.keys())
>>> keys
['hostname', 'username', 'password']
>>> values = list(device.values())
>>> values
['r1', 'usr1234', 'abc123']
>>> items = list(device.items())
>>> items
[('hostname', 'r1'), ('username', 'usr1234'), ('password', 'abc123')]
>>> type(items[0])
<class 'tuple'>

Delete dictionary key

>>> device
{'hostname': 'r1', 'username': 'usr1234', 'password': 'qwe123', 'device_type': 'cisco'}
>>> del device["device_type"]
>>> device
{'hostname': 'r1', 'username': 'usr1234', 'password': 'qwe123'}
>>> device.pop()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: pop expected at least 1 argument, got 0
>>> device.pop("password")
'qwe123'
>>> device
{'hostname': 'r1', 'username': 'usr1234'}
>>> device.pop("password","There is no key named password")
'There is no key named password'

Unpacking dictionaries

>>> device_info = {"hostname":"r1", "device_type":"cisco"}
>>> device_cred = {"username":"usr1234", "password":"Abcd1234"}
>>> device = {**device_info, **device_cred}
>>> device
{'hostname': 'r1', 'device_type': 'cisco', 'username': 'usr1234', 'password': 'Abcd1234'}

Tuples

>>> ifs = tuple( f"ge-0/0/{x}" for x in range(0,23))
>>> ifs
('ge-0/0/0', 'ge-0/0/1', 'ge-0/0/2', 'ge-0/0/3', 'ge-0/0/4', 'ge-0/0/5', 'ge-0/0/6', 'ge-0/0/7', 'ge-0/0/8', 'ge-0/0/9', 'ge-0/0/10', 'ge-0/0/11', 'ge-0/0/12', 'ge-0/0/13', 'ge-0/0/14', 'ge-0/0/15', 'ge-0/0/16', 'ge-0/0/17', 'ge-0/0/18', 'ge-0/0/19', 'ge-0/0/20', 'ge-0/0/21', 'ge-0/0/22')
>>> type(ifs)
<class 'tuple'>
>>> ifs.append("ge-0/0/23")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'append'
>>> ifs.insert("ge-0/0/23")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'insert'
>>> ifs.remove("ge-0/0/0")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'remove'
>>> ifs.pop()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'pop'
>>> ifs.count("ge-0/0/0")
1
>>> len(ifs)
23
>>> min(ifs)
'ge-0/0/0'
>>> max(ifs)
'ge-0/0/9'

Set usage. When we want to insert or append an item to the set, we had error. Another method is add. With add method, items are added to random index of set, not the end of the set. But if we want to pop an item, items popped from beginnig of the set, not from random index. Also, if we want a remove a specific item, just use remove method. Also if we want to extend a set, we can use update method, not extend method like list’s method.

>>> vlans = set(f"vlan_{x}" for x in range(1,11))
>>> vlans
{'vlan_7', 'vlan_5', 'vlan_2', 'vlan_8', 'vlan_3', 'vlan_4', 'vlan_9', 'vlan_1', 'vlan_6', 'vlan_10'}
>>> vlans.append("vlan_11")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'set' object has no attribute 'append'
>>> vlans.insert("vlan_11")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'set' object has no attribute 'insert'
>>> vlans.add("vlan_11")
>>> vlans
{'vlan_7', 'vlan_5', 'vlan_2', 'vlan_8', 'vlan_3', 'vlan_4', 'vlan_9', 'vlan_1', 'vlan_11', 'vlan_6', 'vlan_10'}
>>> vlans.pop("vlan_5")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: set.pop() takes no arguments (1 given)
>>> vlans.pop()
'vlan_7'
>>> vlans.pop()
'vlan_5'
>>> vlans.pop()
'vlan_2'
>>> vlans
{'vlan_8', 'vlan_3', 'vlan_4', 'vlan_9', 'vlan_1', 'vlan_11', 'vlan_6', 'vlan_10'}
>>> vlans.extend({"data","voice","ipcam"})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'set' object has no attribute 'extend'
>>> vlans.update({"data","voice","ipcam"})
>>> vlans
{'vlan_8', 'vlan_3', 'vlan_4', 'data', 'vlan_9', 'vlan_1', 'vlan_11', 'voice', 'vlan_6', 'vlan_10', 'ipcam'}
>>> vlans.remove("vlan_3")
>>> vlans
{'vlan_8', 'vlan_4', 'data', 'vlan_9', 'vlan_1', 'vlan_11', 'voice', 'vlan_6', 'vlan_10', 'ipcam'}
>>> allowed_vlans = {10,20,33}
>>> needed_vlans = {10,50,55,100}
>>> to_config = needed_vlans - allowed_vlans
>>> to_config
{50, 100, 55}

>>> needed_vlans.difference(allowed_vlans)
{50, 100, 55}
>>> needed_vlans.union(allowed_vlans)
{33, 50, 100, 20, 55, 10}
>>> needed_vlans.intersection(allowed_vlans)
{10}