'How to check if dictionary keys match dictionary values in order

I'm trying to see if each key/value pair of a dictionary match, with the resulting output being True or False.

I thought the below code would return True because the keys/values are the same, but False is returned.

path = 'C:\\Users\\path\\to\\directory'
saved_names = os.listdir(path)

cell_names = []

for f in csv_files:
    df = pd.read_csv(f, sep=",", on_bad_lines='skip', header=None)
    myMatrix = df[df.columns[0]].to_numpy()
    cell_names.extend(myMatrix.tolist())

cell_names_csv = list(map(lambda x: x +'.csv', cell_names))
joined_lists = dict(zip(saved_names, cell_names_csv))
print(joined_lists.keys())
print(joined_lists.values())

if joined_lists.keys() == joined_lists.values():
    print(True)
else:
    print(False)

Output:

dict_keys(['weekly report 12457.csv', monthly report 14796.csv', yearly report79652.csv'])
dict_values(['weekly report 12457.csv', monthly report 14796.csv', yearly report79652.csv'])
False

The reason for the False output must be due to the dict_keys and dict_values being held in the variable. Is there a way to remove this? Or an alternative way?



Solution 1:[1]

You can try if list(joined_lists.keys()) == list(joined_lists.values()):

An example demonstration:

>>> my_dict = {"a":"a","b":"b"}
>>> my_dict.keys() == my_dict.values()
False
>>> list(my_dict.keys()) == list(my_dict.values())
True

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dilara Gokay