Question 1: Consider the following list of countries and their capital cities:country_capitals = ["USA: Washington D.C.", "France: Paris", "India: New Delhi", "Russia: Moscow", "China: Beijing"]Create a dictionary using a dictionary comprehension where the country name is the key and the capital city is the value.
Question
Question 1: Consider the following list of countries and their capital cities:country_capitals = ["USA: Washington D.C.", "France: Paris", "India: New Delhi", "Russia: Moscow", "China: Beijing"]Create a dictionary using a dictionary comprehension where the country name is the key and the capital city is the value.
Solution
Here is how you can create a dictionary using a dictionary comprehension:
country_capitals = ["USA: Washington D.C.", "France: Paris", "India: New Delhi", "Russia: Moscow", "China: Beijing"]
# Create a dictionary using dictionary comprehension
country_capital_dict = {item.split(": ")[0]: item.split(": ")[1] for item in country_capitals}
print(country_capital_dict)
In this code, we are using the split(": ") function to separate the country and the capital city in each string. The country (the part before the colon) becomes the key, and the capital city (the part after the colon) becomes the value. The dictionary comprehension {key: value for item in list} creates a new dictionary.
Similar Questions
Question 3: Given a list of cities, create a new list where each element is a tuple containing the city name and its length.cities = ["New York", "London", "Tokyo", "Paris", "Sydney"]
Use chained square brackets to select and print out the capital of France.Create a dictionary, named data, with the keys 'capital' and 'population'. Set them to 'rome' and 59.83, respectively.Add a new key-value pair to europe; the key is 'italy' and the value is data, the dictionary you just built.
Write a function countNow(PLACES) in Python, that takes thedictionary, PLACES as an argument and displays the names (inuppercase)of the places whose names are longer than 5 characters.For example, Consider the following dictionaryPLACES={1:"Delhi",2:"London",3:"Paris",4:"NewYork",5:"Doha"}The output should be:LONDONNEW YORK
Write a Python program to create a new dictionary by extracting the mentioned keys and create a new new dictionary and print it as the result.Constraints:Input Format:Output Format: Example:Input:{"name": "Kelly","age": 25,"salary": 8000,"city": "New york"}["name", "salary"]Output:{'name': 'Kelly', 'salary': 8000}Explanation:Dictionary = {"name": "Kelly","age": 25,"salary": 8000,"city": "New york"}Keys that needs to be extracted = ["name", "salary"]Result = {'name': 'Kelly', 'salary': 8000}Public Test Cases:# INPUT EXPECTED OUTPUT1 {"name": "Kelly","age": 25,"salary": 8000,"city": "New york"} ["name", "salary"] {'name': 'Kelly', 'salary': 8000}
Explain list, tuple, sets and dictionary
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.