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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0