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’]]

11/3/2018 12:59:55 PM


2 Answers

New Answer


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)


What have you tried so far?