Python Forum
Keyword to build list from list of objects?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Keyword to build list from list of objects?
#1
Question 
Hi,

    # Code borrowed from geeksforgeeks.com

    class geeks:
        def __init__(self, name, roll):
            self.name = name
            self.roll = roll

    # creating list
    list = []

    # appending instances to list
    list.append(geeks('Akash', 2))
    list.append(geeks('Deependra', 40))
    list.append(geeks('Reaper', 44))

    # Is there a single instruction to do this?
    namelist = []
    for obj in list:
        namelist.append(obj.name)

    print (namelist)
The above traverses the object list to build a list of one of the parameters of this list.

Is there a built in command that does that? Think

Thanks
Larz60+ write Aug-06-2022, 10:43 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use bbcode tags on future posts.
Reply


Messages In This Thread
Keyword to build list from list of objects? - by pfdjhfuys - Aug-06-2022, 09:56 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  how to get qualified objects in a list Azdaghost 5 751 Apr-22-2025, 06:03 PM
Last Post: Azdaghost
  Strange behavior list of list mmhmjanssen 3 1,759 May-09-2024, 11:32 AM
Last Post: mmhmjanssen
  Why doesn't list require global keyword? johnywhy 9 4,438 Jan-15-2024, 11:47 PM
Last Post: sgrey
  Find a specific keyword after another keyword and change the output sgtmcc 5 2,088 Oct-05-2023, 07:41 PM
Last Post: deanhystad
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 2,777 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 3,576 May-01-2023, 09:06 PM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 1,861 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Сheck if an element from a list is in another list that contains a namedtuple elnk 8 3,658 Oct-26-2022, 04:03 PM
Last Post: deanhystad
  Creating list of lists, with objects from lists sgrinderud 7 3,332 Oct-01-2022, 07:15 PM
Last Post: Skaperen
  Split a number to list and list sum must be number sunny9495 5 4,024 Apr-28-2022, 09:32 AM
Last Post: Dexty

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020