Skip to content
🤔prompts chat🧠
🔍
question:How can I create a Python function that filters a list of dictionaries based on a specific key-value pair and returns the filtered list? For example, if I have a list of dictionaries representing people with their age, I want to filter out only those who are 18 years old.

answer:def filter_by_key_value(dict_list, key, value): Filters a list of dictionaries based on a specific key-value pair. Parameters: dict_list (list): A list of dictionaries. key (str): The key to filter on. value: The value to filter on. Returns: list: A list of dictionaries that match the key-value pair. return [d for d in dict_list if d.get(key) == value]

question:How can I create a Python script to automate the process of checking if a website is up and running? I want to be able to specify a list of URLs and get a report of which ones are accessible and which ones are not.

answer:import requests def check_websites(urls): Given a list of URLs, this function checks if each website is up and running. Returns a dictionary with the URL as the key and 'up' or 'down' as the value. status_report = {} for url in urls: try: response = requests.get(url, timeout=10) if response.status_code == 200: status_report[url] = 'up' else: status_report[url] = 'down' except requests.RequestException: status_report[url] = 'down' return status_report

question:I want to create a simple web application using Flask, a micro web framework in Python. The application should have a main page that displays "Welcome to My Flask App!" and a form where users can input their name. Upon submitting the form, the application should display a personalized greeting, "Hello, [Name]!" on a new page. Can you provide a basic code structure for this application?

answer:from flask import Flask, request, render_template_string app = Flask(__name__) # HTML templates main_page_html = ''' <!doctype html> <html> <head><title>My Flask App</title></head> <body> <h1>Welcome to My Flask App!</h1> <form action="/greet" method="post"> <label for="name">Enter your name:</label> <input type="text" id="name" name="name"> <input type="submit" value="Submit"> </form> </body> </html> ''' greeting_page_html = ''' <!doctype html> <html> <head><title>Greeting Page</title></head> <body> <h1>Hello, {{ name }}!</h1> </body> </html> ''' # Route for main page @app.route('/') def main_page(): return render_template_string(main_page_html) # Route for personalized greeting @app.route('/greet', methods=['POST']) def greet(): name = request.form['name'] return render_template_string(greeting_page_html, name=name) if __name__ == "__main__": app.run(debug=True)

question:Can you help me with a Python script to sort a list of dictionaries based on a specific key's value?

answer:def sort_list_of_dicts(lst, key): Sorts a list of dictionaries based on the given key's value. :param lst: List of dictionaries to be sorted :param key: Key on which to sort the list of dictionaries :return: Sorted list of dictionaries return sorted(lst, key=lambda x: x[key])

Released under the chat License.

has loaded