+ 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 Answers
+ 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