Python Forum
Question about the groupby function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question about the groupby function
#1
Hi, I have the following code:

import itertools
first_letter = lambda x: x[0]
names = ['Alan', 'Adam', 'Wes', 'Will', 'Albert', 'Steven']

for letter, names in itertools.groupby(names, first_letter):
   print(letter, list(names))
The program returned:

A ['Alan', 'Adam']
W ['Wes', 'Will']
A ['Albert']
S ['Steven']

Anybody knows why it does not return the result like the following?

A ['Alan', 'Adam', 'Albert']
W ['Wes', 'Will']
S ['Steven']
Reply


Messages In This Thread
Question about the groupby function - by new_to_python - Feb-08-2020, 04:39 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Question on dir() function Soorya25 1 1,272 Jan-16-2023, 09:33 PM
Last Post: deanhystad
  Function not scriptable: Noob question kaega2 3 1,302 Aug-21-2022, 04:37 PM
Last Post: kaega2
  input function question barryjo 12 3,006 Jan-18-2022, 12:11 AM
Last Post: barryjo
  Use of groupby in a function with Pandas Paulman 0 1,010 Dec-03-2021, 04:56 PM
Last Post: Paulman
  Question on None function in a machine learning algorithm Livingstone1337 1 2,433 Mar-17-2021, 10:12 PM
Last Post: supuflounder
  Question in python function problem saratha 1 1,524 Jul-08-2020, 04:56 PM
Last Post: jefsummers
  question about python3 print function jamie_01 5 2,798 May-25-2020, 09:58 AM
Last Post: pyzyx3qwerty
  Question about list and while function doug2019 6 2,902 Oct-12-2019, 03:07 AM
Last Post: doug2019
  Question on Join() function sduvvuri 2 2,860 Jun-02-2019, 03:55 PM
Last Post: perfringo
  groupby and window function lravikumarvsp 3 3,257 May-13-2018, 06:24 AM
Last Post: buran

Forum Jump:

User Panel Messages

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