Day 10: Mastering Lists in Python for DevOps (Part 2)

CHANDRESH PATLECHANDRESH PATLE
3 min read

Welcome back to our Python for DevOps series! In today's edition, we'll continue our exploration of lists, delving into more advanced concepts such as list comprehensions, and nested lists, and performing sophisticated operations on lists to enhance your DevOps scripting skills.

🔶 List Comprehensions 🔶

🔶 What are List Comprehensions?

List comprehensions provide a concise and expressive way to create lists in Python. They offer a more readable and compact syntax for generating lists based on existing iterables.

# Example of a list comprehension
new_list = [expression for item in iterable if condition]

🔶 Nested Lists and Advanced Operations

1. Nested Lists:

A nested list is a list that contains other lists as its elements. This structure allows for more complex data organization.

# Example of a nested list
matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

2. Advanced List Operations:

  • Sorting a List:
# Sorting a list
my_list.sort()
  • Reversing a List:
# Reversing a list
my_list.reverse()
  • Finding the Maximum and Minimum:
# Finding the maximum and minimum
max_value = max(my_list)
min_value = min(my_list)

🔶 Practice Exercises and Examples

Example: Print a List of Files in the List of Folders Provided

# DevOps script to print a list of files in the list of folders provided
# Import the os module for interacting with the operating system
import os
# Prompt the user to enter a list of folder paths separated by spaces
folder = input("Enter a list of folder paths separated by spaces: ").split()

# List comprehension to create a list of files in each folder
for folder in folder:
    files = os.listdir(folder)
    # Printing the result of folder name
    print("listing files for the folder: " + folder)

    for file in files:
    # Printing the result
        print(file)

# Import the os module for interacting with the operating system
import os
# Prompt the user to enter a list of folder paths separated by spaces
folders = input("Enter a list of folder paths separated by spaces: ").split()
# Iterate through each folder path provided by the user
for folder in folders:
    try:
        files = os.listdir(folder)   # Get the list of files in the current folder
        # Print the folder path to indicate the following list of files belongs to this folder
        print("Listing files for the folder: " + folder)

     # Handle the case where the specified folder is not found
    except FileNotFoundError:
        print("Error: Folder not found - " + folder)
    # Handle the case where there's a permission issue accessing the folder
    except PermissionError:
        print("Error: Permission denied - " + folder)

    print("\n")  # Adding a line break for better readability

    # Continue to the next iteration of the loop, even if an exception occurred
    continue     
# Iterate through each file in the current folder and print its name
for file in files:
        print(file)

🔶 Conclusion

Mastering list comprehensions and advanced list operations empowers DevOps engineers to handle complex data structures efficiently. These techniques enhance the readability and maintainability of your scripts. Stay tuned for more advanced topics in our Python for DevOps journey!

Note: I am following Abhishek Verraamalla's YouTube playlist for learning.

GitHub Repo: https://github.com/Chandreshpatle28/python-for-devops-av


Happy Learning :)

Stay in the loop with my latest insights and articles on cloud ☁️ and DevOps ♾️ by following me on Hashnode, LinkedIn (https://www.linkedin.com/in/chandreshpatle28/), and GitHub (https://github.com/Chandreshpatle28).

Thank you for reading! Your support means the world to me. Let's keep learning, growing, and making a positive impact in the tech world together.

#Git #Linux Devops #Devopscommunity #PythonforDevOps #python

1
Subscribe to my newsletter

Read articles from CHANDRESH PATLE directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

CHANDRESH PATLE
CHANDRESH PATLE

Hi, I'm Chandresh Patle, an aspiring DevOps Engineer with a diverse background in field supervision, manufacturing, and service consulting. With a strong foundation in engineering and project management, I bring a unique perspective to my work. I recently completed a Post Graduate Diploma in Advanced Computing (PG-DAC), where I honed my skills in web development, frontend and backend technologies, databases, and DevOps practices. My proficiency extends to Core Java, Oracle, MySQL, SDLC, AWS, Docker, Kubernetes, Ansible, Linux, GitHub, Terraform, Grafana, Selenium, and Jira. I am passionate about leveraging technology to drive efficient and reliable software delivery. With a focus on DevOps principles and automation, I strive to optimize workflows and enhance collaboration among teams. I am constantly seeking new opportunities to expand my knowledge and stay up-to-date with the latest industry trends. If you have any questions, collaboration ideas, or professional opportunities, feel free to reach out to me at patle269@gmail.com. I'm always open to connecting with fellow tech enthusiasts and exploring ways to contribute to the DevOps community. Let's build a better future through innovation and continuous improvement!