Skip to main content

Python Lists

Python lists are versatile and can be used to represent various cyber security data such as HTTP status codes, protocols, IP addresses, and more. In this tutorial, we'll delve deep into the functionalities of Python lists and their applications in the realm of cyber security.

Introduction to Lists

What you'll learn:

  • Creating a list
  • Printing a list
print("List of HTTP Status Codes")
responses = [200, 400, 403, 500]
print(responses)

Accessing List Elements

What you'll learn:

  • Accessing elements using positive indices
  • Accessing elements using negative indices
HTTPOk = 200
print(HTTPOk)
print("HTTP OK Status Codes", str(HTTPOk)) # Convert to string
print(responses[0]) # First element
print(responses[1]) # Second element
print(responses[-1]) # Last element
print(responses[-2]) # Second last element

Modifying Lists

What you'll learn:

  • Appending to a list
  • Inserting into a list
  • Removing from a list
protocolList = ["http", "https", "ftp", "sftp", "ssh", "telnet", "smtp"]
protocolList.append("snmp")
protocolList.insert(0, 'tcp')
protocolList.pop()
protocolList.remove('tcp')
protocolList.pop(1)

Searching in Lists

What you'll learn:

  • Finding the index of an element
  • Counting occurrences of an element
print(protocolList)
protocolList.index('snmp')
protocolList.count('snmp')

Sorting and Reversing Lists

What you'll learn:

  • Reversing a list
  • Sorting a list in ascending and descending order
protocolList.reverse()
protocolList.sort(reverse=True)
protocolList.sort()

Slicing Lists

What you'll learn:

  • Slicing a list
  • Slicing a list with a step
print(protocolList[0:3])
print(protocolList[0:3:2])