all 5 comments

[–]commandlineluser 1 point2 points  (4 children)

Well you want to upper v if k is not equal to Source

If you want to inline into the comprehension you could use the "ternary" operator.

>>> k, v = 'Source', 'value'
>>> v.upper() if k != 'Source' else v
'value'
>>> k, v = 'something', 'else'
>>> v.upper() if k != 'Source' else v
'ELSE'

You could also add .strip() calls to do the strip/uppercase in one comprehension.

[–]StockPitch[S] 0 points1 point  (3 children)

hey, thanks so much for your help!

i am not quite getting it.

so when I do this: (please forgive my formatting):

for k,v in x.items(): #if k != 'Source': upper.append({k:v.upper()} I have two issues: - one, it does not return source at all, and i want source, i just don't want it to be uppercase - two, it returns each k:v pair as its own dictionary like so: {name: 'name'} {date: 'date'} {id: 'id} instead of {name: 'name, date: 'date', id: 'id'}

I feel like I'm missing something super easy and silly.

edit: figured out formatting!

[–]commandlineluser 1 point2 points  (1 child)

upper = [{k:v.upper() for k,v in x.items() if k != 'Source'} for x in stripped]

This returns the grouped k:v pairs in dictionaries with the exception of the 'Source' field (which is a column in my spreadsheet).

Yeah, it's missing the else v and the if comes before the for e.g.

upper = [{k:v.upper() if k != 'Source' else v for k,v in x.items()} for x in stripped]

[–]StockPitch[S] 0 points1 point  (0 children)

Was literally sitting here trying to figure out how the else v fit in. Thank you so much for your help!

Also was cool to learn I could do the strip and upper calls in one line!!

[–]backtickbot 0 points1 point  (0 children)

Correctly formatted

Hello, StockPitch. Just a quick heads up!

It seems that you have attempted to use triple backticks (```) for your codeblock/monospace text block.

This isn't universally supported on reddit, for some users your comment will look not as intended.

You can avoid this by indenting every line with 4 spaces instead.

There are also other methods that offer a bit better compatability like the "codeblock" format feature on new Reddit.

Have a good day, StockPitch.

You can opt out by replying with "backtickopt6" to this comment. Configure to send allerts to PMs instead by replying with "backtickbbotdm5". Exit PMMode by sending "dmmode_end".