Reputation: 99
I have a dictionary, what
:
what = {'a': ['b'], 'c': ['d\n', 'e\n', 'f'], 'd': ['f', 'g']}
and need to get the items with '/n' separate and without '/n' (order is important, need to sort the values.):
[[{'a': ['b'], 'c': ['f'], 'd': ['f', 'g']}], [{'c': ['d\n', 'e\n']}]]
This is what I tried:
def lst(profiles_file: TextIO):
solve_lst = []
new_lst = fix_files(profiles_file)
for k, v in new_lst.items():
for i in v:
if i.find('\n') != -1:
get_index = [v.index(i)]
solve_lst.append(get_index)
return solve_lst
How can i get this without doing anything to complicated?
Upvotes: 2
Views: 52
Reputation: 61063
Here's a solution using defaultdict
from collections import defaultdict
def lst(profiles_file: TextIO):
initial_dict = fix_files(profiles_file)
with_n = defaultdict(list)
without_n = defaultdict(list)
for k, v in initial_dict.items():
for item in v:
if '\n' in item:
with_n[k].append(item)
else:
without_n[k].append(item)
return [without_n, with_n]
Upvotes: 1