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
  Strange behavior list of list mmhmjanssen 2 253 May-01-2024, 07:16 AM
Last Post: Gribouillis
  Why doesn't list require global keyword? johnywhy 9 902 Jan-15-2024, 11:47 PM
Last Post: sgrey
  Find a specific keyword after another keyword and change the output sgtmcc 5 883 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 1,248 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,595 May-01-2023, 09:06 PM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 947 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 1,903 Oct-26-2022, 04:03 PM
Last Post: deanhystad
  Creating list of lists, with objects from lists sgrinderud 7 1,724 Oct-01-2022, 07:15 PM
Last Post: Skaperen
  Split a number to list and list sum must be number sunny9495 5 2,350 Apr-28-2022, 09:32 AM
Last Post: Dexty
  How to store the resulting Doc objects into a list named A xinyulon 1 1,923 Mar-08-2022, 11:49 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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