What is more "Pythonic" - dict comprehsion with list generator, or an explicit for loop?

what is more synonym
what is more crossword
what is more transition
moreover
furthermore
what is more meaning in hindi
what is mores
define additional

Suppose I have a function with input 'raw_data' raw_data consists of lines such as:

key1: str1  
key2: str2  
...  

Where strx is of the form aa:bb:cc:dd... - that is a ':' separated string There is a helper function which does something to the strings, converting them to values, let's call it get_value()

What would be the most pythonic way to return a dict?

to_dict(raw_data):
  list_data = raw_data.splitlines()
  return { key.replace(':',''): get_value(str) for key,str in (line.split() for line in list_data)}

or

to_dict(raw_data):
  list_data = raw_data.splitlines()
  mydict = {}
  for line in list_data:
    key,str = line.split()
    key = key.replace(':', '')
    mydict.update({ key: get_value(str)})
  return mydict

Or is there some much more pythonic way of doing this? I'm aware this might seem opinion based question, but there seems to be a consensus about what is 'more pythonic' or 'less pythonic' way of doing things, I just don't know what the consensus is in this case.

Your first expression is more "Pythonic". If you're worried about readablity, you can take the generator expression out onto its own line.

def to_dict(raw_data):
    data = (line.split(': ', 1) for line in raw_data.splitlines())
    return {key: get_value(st) for key, st in data}

line.split(': ', 1) will split line on only the first ": ", so "a: b: c" becomes ['a', 'b: c']

What is more what's more definition and meaning, phrase. You can use what is more or what's more to introduce an extra piece of information which supports or emphasizes the point you are making. [emphasis] He was now a king, and what was more, a well-beloved king. Furthermore; the next aspect or fact is equally or more important. Based on our testing, this blender performed the best. What's more, it is half the price than most of the other models!

How about:

{k:v for l in raw_data.splitlines() for (k,v) in [l.split(":")]}

need I say more, What's more is an expression that's used when you want to emphasize that the next action or fact is more or as important as the one mentioned. War doesn't bring peace; what's more, it brings more chaos. a greater quantity, amount, or degree: More is expected of him. The price is more than I thought. adverb compar. of much with most as superl.

List Comprehension is really a handy and faster way to write for loops in Python in just a single line of code. The idea of comprehension is not just unique to lists in Python. Dictionaries, one of the commonly used data structures in data science, can also do comprehension. This is called dict comprehension or dictionary comprehension.

Remember that, in python a list is defined with square brackets [] and a dictionary is defined with curly braces {}. The same idea is carried over in defining dict comprehension as well. dict comprehension is defined with a similar syntax, but with a key:value pair in expression.

What is more, what's more meaning: 1. used to add something surprising or interesting to what you have just said: 2. used to add…. Learn more. Define what is more. what is more synonyms, what is more pronunciation, what is more translation, English dictionary definition of what is more. Adv. 1. what is more - in addition; "computer chess games are getting cheaper all the time; furthermore, their quality is improving"; "the cellar was dark;

WHAT'S MORE, Define WHAT'S MORE (phrase) and get synonyms. What is WHAT'S MORE (​phrase)? WHAT'S MORE (phrase) meaning, pronunciation and more by Macmillan  what's more definition: 1. used to add something surprising or interesting to what you have just said: 2. used to add…. Learn more.

WHAT'S MORE (phrase) definition and synonyms, Synonyms for what is more at Thesaurus.com with free online thesaurus, antonyms, and definitions. Find descriptive alternatives for what is more. Definition of what is more in the Idioms Dictionary. what is more phrase. What does what is more expression mean? Definitions by the largest Idiom Dictionary.

What is more Synonyms, What is more Antonyms, Adv. 1. what is more - in addition; "computer chess games are getting cheaper all the time; furthermore, their quality is improving"; "the cellar was dark; moreover,  or what's more. phrase. You can use what is more or what's more to introduce an extra piece of information which supports or emphasizes the point you are making. [emphasis] He was now a king, and what was more, a well-beloved king. You should remember it, and what's more, you should get it right. See full dictionary entry for more.

Comments
  • the dictionary case is the pythonic way to go.
  • dict should be key: value, but otherwise this answer works great for me, thanks.
  • You have your loops backwards. They execute from left to right.
  • I'm sorry, forgot to mention the strings have ':'s in them, updated the question to reflect this
  • Thanks Patrick, yes you're quite right - have edited to correct accordingly - I also noticed I had to wrap the l.split(":") component in list brackets to avoid the ValueError: not enough values to unpack (expected 2, got 1) error I was getting when trying to fill (k,v) from the split contents. I'll confess, I'm not entirely sure why that's necessary, but it looks as though it is in this case.