-1
def group_together(players, team):
    coplayers = list(players)
    players = []
    players_team = []
    num_players= len(coplayers)
    no_groups = num_players // team
    if num_players%team > 0:
        no_groups += 1
    c = 0
    for i in range(no_groups):
        players_team.append([])
        for j in range(team):
            if c < num_players:
                players_team[i].append(coplayers[c])
                c+=1
    players = players_team

players = [456, 218, 67, 1, 101, 199]
group_together(players, 2)#nothing is returned
print(players)

Output:

[[456, 218], [67, 1], [101, 199]]

I'm trying to replace the players with the new groupings of it but when I run the code with the test code it does not print the new players in groups, but instead, it prints the old unmodified players.

Is there any way to fix this? If so I would be very grateful.

Hackaholic
  • 19,069
  • 5
  • 54
  • 72
Yergia
  • 7
  • 1
  • Python doesn't have call-by-reference semantics. If you want a value back from a function, you need to return it. – Silvio Mayolo Oct 19 '21 at 01:45
  • But I am not trying to return it I'm trying to modify players. – Yergia Oct 19 '21 at 01:46
  • What is expected output? – Jab Oct 19 '21 at 01:47
  • I want the outcome to be [[456, 218], [67, 1], [101, 199]] but it comes out as [456, 218, 67, 1, 101, 199] instead. – Yergia Oct 19 '21 at 01:48
  • I tried using players.clear() but the output comes out as empty [] – Yergia Oct 19 '21 at 01:49
  • if I do players.clear() is there a way to make sure that players = players_team does not cause a problem? – Yergia Oct 19 '21 at 01:51
  • I know that using extend works but how do I do it without using extend? – Yergia Oct 19 '21 at 01:53
  • 1
    @SilvioMayolo that's true in most cases, but because lists are mutable you can do it. Just take out the `players = []` and use `players[:] = players_team` at the end. – Mark Ransom Oct 19 '21 at 01:54
  • then is there a way to put them into groupings without slicing and using extend? – Yergia Oct 19 '21 at 01:55
  • Without using slicing or extend you can't modify the list that was passed to you. Reassigning it the way you did only changes the list you see inside the function, not what you see outside the function. – Mark Ransom Oct 19 '21 at 01:56
  • See [How do I pass a variable by reference?](https://stackoverflow.com/q/986006/5987) – Mark Ransom Oct 19 '21 at 02:00
  • 2
    Why all of the artificial restrictions? You refuse to use function return values, slice notation, or the `extend` function? Add a few more restrictions and this question would be more appropriate as a restricted-source challenge on CodeGolf SE. – Silvio Mayolo Oct 19 '21 at 02:10

1 Answers1

0

'players' it's considered as new local variable inside the function. Instead of players=[] you can use players.clear(), and players.append(players_team)to assign the value

def group_together(players, team):
    coplayers = list(players)
    players.clear()
    players_team = []
    num_players= len(coplayers)
    no_groups = num_players // team
    if num_players%team > 0:
        no_groups += 1
    c = 0
    for i in range(no_groups):
        players_team.append([])
        for j in range(team):
            if c < num_players:
                players_team[i].append(coplayers[c])
                c+=1
    players.append(players_team)
Genti
  • 1
  • 1