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

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

ยท

3 min read

Play this article

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

Did you find this article valuable?

Support CHANDRESH PATLE by becoming a sponsor. Any amount is appreciated!

ย