I have a list of members, which is obtained in this way:

members = Organization.get_members(org1) 

where Organization is a class, get_members() its field. It all works.

Now I need to display the names of all users in the members list and automatically assign them a number from 0 to (the number of users in the list is 1), so that 0 matches members[0].full_name ; 1 - members[1].full_name .

Ie should get a sign:

 Asignee: 0-Roman, 1-Serg, 2-Anton … 

and so on, where the names are members[index].full_name , and the index value must match the number before the hyphen.

  • 3
    Well, what's the problem then? - Enikeyschik
  • @ Enikeyschik in this and the problem is that 0 corresponds to the members member under the 0 index in the list of members, etc. to all members. Ie, the program should display users exactly as many as they are in the members, while automatically determining how many of them are there. - Sergiy Baraban
  • Judging by the code snippet, you are not the first day with a python. Therefore, it is strange that you ask how to list the elements of the list individually ... - Enikeyschik
  • Not the first day, but the answer is @Andrio Skur (use enumerate) solved my problem. Live and learn, as they say. - Sergiy Baraban
  • Yes, you can not use. - Enikeyschik 2:41 pm

2 answers 2

If I understood correctly, then:

 print(*enumerate(member.full_name for member in Organization.get_members(org1)), sep='\n') 
  • this is close, but then the result is (0, <Member 5965ea0f40c5feef6a2741f8>) (1, <Member 52551dedeb7f848b0500060f>), but I need to have (0, Roman), (1, Serhii), instead of the figure, the object was not displayed, but the full name. - Sergiy Baraban 2:12 pm
  • one
    for i, item in enumerate (members): print ("{} - {}". format (i, members [i] .full_name)) - Sergiy Baraban

When you want to get a list of vapors, you can do this:

 result = [(i, member.full_name) for i, member in enumerate(members)] 

Explanation:

enumerate(members) will add a sequence number for every list object members , and a pair

  (i, member.full_name) 

uses these ordinal numbers directly , but instead of a private object ( member ) it will substitute its attribute ( full_name ).