-1

I adapted a function to solve a problem in my project but I don't really get how it works. Btw a user list is stored in self.__users, it is read from users.txt.

def delete(self, nick):
    for x, u in enumerate(self.__users):    # <----- Just this line
        if u._nickname == nick:
            del self.__users[x]
            return

Does this mean it will separate each user and give them a number? I don't understand that line.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
bonifacil
  • 13
  • 3

2 Answers2

0

Enumerate generates two variables from an array, The first one is an array element's index, the second one is its element itself. So for array ['x', 'y', 'z' ], the initial values for these variables will be 0 and 'x' respectively.

Yarh
  • 895
  • 7
  • 15
  • 1
    `enumerate` returns a tuple where the first element is the index and the second element the current value from the iterable. So switching the usage of `x` and `u` as you suggested is false. – Matthias Dec 11 '20 at 21:37
0

Yep... the enumerate built-in function will create a list something like:

[(0, user1),
 (1, user2),
 (2, user3)]

Note that I don't know any info about your self.__users that's why I putting that way.... So this line for x, u in enumerate(self.__users): will return for x the index and for the u your user.

Hope that helped

  • 1
    In [How to Answer](https://stackoverflow.com/help/how-to-answer), see the section *Answer Well-Asked Questions*, and therein the bullet point regarding questions that "have been asked and answered many times before". – Charles Duffy Dec 11 '20 at 21:31
  • @CharlesDuffy thanks... I'll read that and try to make better next time – Mario H. Adaniya Dec 11 '20 at 21:50