Skip to content

Instantly share code, notes, and snippets.

@mei-li
Created August 29, 2021 10:37
Show Gist options
  • Save mei-li/a570e893ab9d42d0f83a1fe6baa91413 to your computer and use it in GitHub Desktop.
Save mei-li/a570e893ab9d42d0f83a1fe6baa91413 to your computer and use it in GitHub Desktop.
# Apo list tuples se dictionary key: lista. eg
# results = [("word1", "explaination1"), ("word2", "explaination2"), ("word1", "explaination12")]
# ->
# data = {"word1": ["explaination1", "explaination12"], "word2": explaination2}
data = {}
for i in results:
data.setdefault(i[0],[]).append(i[1])
# exigisi:
# to data.setdefault vazei sto key word1 mia adeia lista kai an exei idi kati den kanei tipota,
# oste na einai idi lista gia na kanei append tin exigisi
# Ενας εναλλακτικός τρόπος να γραφτει το ιδιο πιο αναγνώσιμο
data = {}
for word, explanation in results:
data.setdefault(word,[]).append(explanation)
# Allos tropos
data = defaultdict(list) # auto leei oti ta values autou tou dict einai adeies listes otan den uparxoun
for word, explanation in results:
data[word].append(explanation)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment