How can i create a list (new) from an existing list where items of list(new) will be list in python

Funny Boss

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?

Pavan Skipo

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]

編集
0

コメントを追加

0

関連記事

分類Dev

How to create new list from list of list where elements are in new list are in alternative order?

分類Dev

How to make nested list from list where values are counts of list items Python

分類Dev

How can I create a map with every two items in a list in Kotlin?

分類Dev

How to collect a new list from a list in python

分類Dev

How can I implement the ability to add a new task to an existing list (project) or to a new one

分類Dev

How can I successfully capture all possible cases to create a python list from a text file

分類Dev

How to get items out of a list and create a new one in Ansible

分類Dev

(Python) How can I convert a list of integers into a list of sets where each set is a pair of these integers?

分類Dev

How can I create a list pulling data from another list of objects

分類Dev

make new python lists with indexes from items in a single list

分類Dev

How to create a dictionary using python list comprehension from 2 list

分類Dev

How can I extract list from list in prolog?

分類Dev

How can I slice a list based on the lengths from another list?

分類Dev

How I can remove more than 1 items from drop down list?

分類Dev

How can I change the number of items that end up in a list

分類Dev

Is there a way to automatically create categories from a list of items?

分類Dev

Where can I find a list of recent updates from Microsoft?

分類Dev

How do I Create a new page by clicking on my list?

分類Dev

Where can I find a list of possible exceptions in Python?

分類Dev

Python items in a list still a list?

分類Dev

Can I display multiselect list items as onordered list using Razor?

分類Dev

How can I use a list of values with the IN operator in a WHERE clause?

分類Dev

Python - Can't create instances from a list or random instances

分類Dev

Remove unnecessary items from python list

分類Dev

How do I create a list of a type that can hold anything available?

分類Dev

How can I programmatically create a list of objects of the leaflet icon class?

分類Dev

How can I list data and create a summary table?

分類Dev

How can I create a list divider for a collapsible set in jquery mobile

分類Dev

Python3 Create Array from List (I Think)

Related 関連記事

  1. 1

    How to create new list from list of list where elements are in new list are in alternative order?

  2. 2

    How to make nested list from list where values are counts of list items Python

  3. 3

    How can I create a map with every two items in a list in Kotlin?

  4. 4

    How to collect a new list from a list in python

  5. 5

    How can I implement the ability to add a new task to an existing list (project) or to a new one

  6. 6

    How can I successfully capture all possible cases to create a python list from a text file

  7. 7

    How to get items out of a list and create a new one in Ansible

  8. 8

    (Python) How can I convert a list of integers into a list of sets where each set is a pair of these integers?

  9. 9

    How can I create a list pulling data from another list of objects

  10. 10

    make new python lists with indexes from items in a single list

  11. 11

    How to create a dictionary using python list comprehension from 2 list

  12. 12

    How can I extract list from list in prolog?

  13. 13

    How can I slice a list based on the lengths from another list?

  14. 14

    How I can remove more than 1 items from drop down list?

  15. 15

    How can I change the number of items that end up in a list

  16. 16

    Is there a way to automatically create categories from a list of items?

  17. 17

    Where can I find a list of recent updates from Microsoft?

  18. 18

    How do I Create a new page by clicking on my list?

  19. 19

    Where can I find a list of possible exceptions in Python?

  20. 20

    Python items in a list still a list?

  21. 21

    Can I display multiselect list items as onordered list using Razor?

  22. 22

    How can I use a list of values with the IN operator in a WHERE clause?

  23. 23

    Python - Can't create instances from a list or random instances

  24. 24

    Remove unnecessary items from python list

  25. 25

    How do I create a list of a type that can hold anything available?

  26. 26

    How can I programmatically create a list of objects of the leaflet icon class?

  27. 27

    How can I list data and create a summary table?

  28. 28

    How can I create a list divider for a collapsible set in jquery mobile

  29. 29

    Python3 Create Array from List (I Think)

ホットタグ

アーカイブ