+ 1

make a group of lists...

Write a piece of code which will make a group of lists from a given list that each of them contains elements from the original list with the same first character. Example: if the given list is [‘ali’, ‘apple’, ‘anathema’, ‘bee’, ‘boz’, ‘kafka’] then result is [[‘ali’, ‘apple’, ‘anathema’], [‘bee’, ‘boz’], [‘kafka’]]

3rd Nov 2018, 12:59 PM
shima
shima - avatar
2 RĂ©ponses
+ 1
source = [ 'apple', 'apricot', 'berry', 'banana'] destination = [] for letter in 'abcdefghijklmnopqrstuvwxyz': tmp = [element for element in source if element.startswith(letter)] if tmp: destination.append(tmp)
3rd Nov 2018, 3:49 PM
HonFu
HonFu - avatar
0
What have you tried so far?
3rd Nov 2018, 2:55 PM
Russ
Russ - avatar