Python program combine 2 dictionary's

Currently reading:
 Python program combine 2 dictionary's

yohilax951

Member
LV
1
Joined
Dec 20, 2023
Threads
63
Likes
32
Awards
5
Credits
3,810©
Cash
0$
import json

def combine_and_save_dicts():
# Input paths for dictionaries
dict1_path = input("Enter path for dictionary one: ")
dict2_path = input("Enter path for dictionary two: ")

# Load dictionaries from files
with open(dict1_path, 'r') as file1:
dict1 = json.load(file1)

with open(dict2_path, 'r') as file2:
dict2 = json.load(file2)

# Combine dictionaries
combined_dict = {**dict1, **dict2}

# Save combined dictionary to output.txt
with open('output.txt', 'w') as output_file:
json.dump(combined_dict, output_file, indent=4)

print("Dictionaries combined and saved to output.txt")

# Run the function

combine_and_save_dicts()
 

yohilax951

Member
LV
1
Joined
Dec 20, 2023
Threads
63
Likes
32
Awards
5
Credits
3,810©
Cash
0$
import json

def combine_and_save_dicts():
# Input paths for dictionaries
dict1_path = input("Enter path for dictionary one: ")
dict2_path = input("Enter path for dictionary two: ")

# Load dictionaries from files
with open(dict1_path, 'r') as file1:
dict1 = json.load(file1)

with open(dict2_path, 'r') as file2:
dict2 = json.load(file2)

# Combine dictionaries
combined_dict = {**dict1, **dict2}

# Save combined dictionary to output.txt
with open('output.txt', 'w') as output_file:
json.dump(combined_dict, output_file, indent=4)

print("Dictionaries combined and saved to output.txt")

# Run the function

combine_and_save_dicts()
Thanks
 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

Similar threads

Top Bottom