简体   繁体   中英

Can a python list be made with two other lists?

So for example this code here:

one = [0,1]
two = [2,3]
three = [one, two] 

Is that possible?

Yes. As student said, definitely try the output in your python interactive ide before posting. That being said, you can even do:

three = one + two

Using [one, two] , it will write the lists in that list eg [[0],[1]] . If you want a single list then add them like one + two .

>>> one = [0,1]
>>> two = [2,3]
>>> three = [one, two]
>>> three
[[0, 1], [2, 3]]
>>>
>>> three = one + two
>>> three
[0, 1, 2, 3]
>>> 

You can do + operator:

three = one+two

Or if version is > python 3:

three = [*one, *two]

Or can do extend :

three=one.copy()
three.extend(two)

In all examples:

print(three)

Will output:

[0,1,2,3]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM