+ 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â]]
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)
0
What have you tried so far?