简体   繁体   中英

How to create a list of tuples in python?

I have a function which accepts a list of tuples. I only have an array of integers and want to convert it into list.

I have this:

[1,2,3]

What I want:

[(1),(2),(3),(4)]

You can create a list of tuples using a list comprehension:

>>> tuples = [(x,) for x in [1, 2, 3]]
>>> print(tuples)
[(1,), (2,), (3,)]

The comma in (x,) is the tuple operator which makes the comprehension create tuples rather than just ints.

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