How to combine a group of lists together

Cameron Simpson cs at cskk.id.au
Sat Dec 21 00:31:36 EST 2019


On 20Dec2019 21:04, wanghao7915 at gmail.com <wanghao7915 at gmail.com> wrote:
>I utilized .get_text() + .split() functions and obtain this:
>spe=
>['$278.86as', 'of', 'Dec', '20,', '2019,', '06:47', 'PST', '-', 'Details']
>['4.7', 'inches']
>['750', 'x', '1334']
>['64', 'GB']
>['2', 'GB']
>['Apple', 'A11', 'Bionic,', 'Hexa-Core,', '(2x', 'Monsoon', '+', '4x', 'Mistral)']
>['5.22', 'ounces']
>['No']
>['No']
>['12', 'MP']
>['7', 'MP']
>['1821', 'mah']
>['No']
>['Yes,', 'on', 'the', 'front']
>['None']
>['iOS', '(Last', 'OS', 'info:', 'IOS', '11)']
>['September', '2017']
>
>They have a shared name 'spe', but no concrete names for each list.

Does that mean you have a loop, with spe being computed in each loop 
pass?

It would help to see your code.

>If I use spe[0], I can only get all the first element of each list.

Indeed, since that designates the first element.

>How can I mix all of these elements into a single list like:
>['$278.86as', 'of', 'Dec', '20,', '2019,', '06:47', 'PST', '-', 
>'Details','4.7', 'inches'......]

Have a look at the chain() function from the itertools module. Or at the 
extend() method of the list class. Either will help you compose a longer 
list from a collection of individual lists.

If you're still stuck, post your code in a followup message and ask 
sepecific questions about your attempts to build a bigger list from 
various smaller ones.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Python-list mailing list