0

I have an array of objects

[<obj1>, <obj2>, <obj3>, <obj4>]

I want to get something like

[obj1.name, obj2.name, obj3.name, obj4.name]

I do not want to use loop for this, just looking for some more efficient way to get this

1
  • "I do not want to use loop for this" - why? Commented Dec 9, 2020 at 10:20

2 Answers 2

2

Turning @Tomerikoo's comment into an answer:

Use a list comprehension:

[o.name for o in l]

Note that this will not necessarily be more efficient than looping, but obviously more beautiful.

Sign up to request clarification or add additional context in comments.

1 Comment

2

I think this will help you:

l = [<obj1>, <obj2>, <obj3>, <obj4>]
list(map(lambda x:x.name, l))

2 Comments

@Tomerikoo, Yes, sorry. edited.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.