I wonder if the list can be made into a tuple.

Asked 2 years ago, Updated 2 years ago, 16 views

I'm posting a question because I wonder if I can make a list into a tuple.

l = [4,5,6]
t = (4,5,6)

I want to make a tuple t with a list l, but the source code I wrote does not print a tuple, but it is printed as a generator object.

l = [4,5,6]

t = (i for i in l)
print(t)

Output:

<generator object <genexpr> at 0x10e92dd20>

python

2022-09-22 10:53

1 Answers

Use tuple() to convert list to tuple, just as you use int(3") to convert str "3" to int3.

l = [[4,5],6]
t = tuple(l)
print(t)

Output: ([4, 5], 6)


2022-09-22 10:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.