1. import os
  2. def remove_duplicate_env_vars(env_list):
  3. """
  4. Removes duplicate environment variables from a list,
  5. using default values for duplicates.
  6. """
  7. unique_vars = {} # Use a dictionary to track unique variables
  8. for var in env_list:
  9. if var in unique_vars:
  10. continue # Skip if already seen
  11. else:
  12. unique_vars[var] = os.getenv(var, "default_value") # Get value or default
  13. return unique_vars
  14. if __name__ == '__main__':
  15. # Example usage:
  16. # Simulate environment variables (replace with your actual list)
  17. env_vars = ["API_KEY", "DEBUG", "API_KEY", "DATABASE_URL", "DEBUG", "PORT"]
  18. cleaned_vars = remove_duplicate_env_vars(env_vars)
  19. # Print the cleaned environment variables
  20. for var, value in cleaned_vars.items():
  21. print(f"{var}={value}")

Add your comment