def group_by_owners(files):
fin2=[]#creating list
fin={}#creating dictionary
for v in files.values():#looping the values of a dictionary
for m,n in files.items():#looping the keys of dictionary
if n==v:
fin2.append(m)#appending all file_name which belong to owner
fin[v]=fin2#storing in dictionary
fin2.clear()#the line where i have used clear() function
return fin
files = {'Input.txt': 'Randy','Code.py': 'Stan','Output.txt': 'Randy'} #file_name:Owner
group_by_owners(files)
print(group_by_owners(files))
output:{'Randy': [], 'Stan': []}
def group_by_owners(files):
fin2=[]
fin={}
for v in files.values():
for m,n in files.items():
if n==v:
fin2.append(m)
fin[v]=fin2
fin2=[] #line where i have reinitialized the list as empty
return fin
files = {'Input.txt': 'Randy','Code.py': 'Stan','Output.txt': 'Randy'}
group_by_owners(files)
print(group_by_owners(files))
output:{'Randy': ['Input.txt', 'Output.txt'], 'Stan': ['Code.py']}
Both clear() function and list=[] Do The Same Job Of Emptying The List Then Why Does it Give A Different Output