I have a list like the following:
original_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
And my desired output is:
result = [['a', 'b'], ['c', 'd'], ['e', 'f'], ['g']]
Here, the items of 'result' will be each one list of two items from the original_list. so if the total no of items in original_list is 8 then it will give like the following:
original_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
And my desired output is:
result = [['a', 'b'], ['c', 'd'], ['e', 'f'], ['g', 'h']]
I tried to solve that using 2 dimensional matrix but its not working as the elements of the original_list is sometimes even and odd.
any suggestion how can i follow?
Is this what you want?
original_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
result = []
for i in range(0, len(original_list), 2):
result.append(original_list[i:i+2])
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加