2

I haven't been able to find this anywhere else. I have

lst = [
  {'n':'Ste', ...}
  ,{'n':'Mil', ...}
  ,{'n':'Tob', ...}
  ,{'n':'', ...}
  ,{'n':'Ste', ...}
  ,{'n':'Mil', ...}
]

How can I reduce this to a distinct

['Ste','Tob','Mil','']

Thanks.

2 Answers 2

3

It is called set comprehension

{ l['n'] for l in lst }
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks. It is really simple once you see it.
note that it needs to be converted back to a list if you want to respect OP output (but that's minor since order is not guaranteed by the dictionary at start!). A little more explanation would be good, but still good answer.
Drop a line of code to show what you are thinking of?
just list({ l['n'] for l in lst }). But really, minor, as I said.
@Jean-FrançoisFabre My final had to be list(set({ l['n'] for l in lst })) which surprised me. I thought it was already a set. So for some reason I had to make a set of a set before I could convert it to a list?
|
0

Looks like the user needs a list.

list({ l['n'] for l in lst })

p.s. this would've been a comment if i had enough reps.

3 Comments

You are correct. I drew the desired as a list. But list, set, other. ot me it is more about the distinct. Thanks.
you'll be able to comment when you have enough rep. That said, it won't be with useless answers like that.
@Jean-FrançoisFabre alright. wont forget. although i dont see it as useless. maybe its just my perspective and not yours. thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.