1

I have this list of numbers in python

['1', '0', '0', '0', '1', '1', '4']

And I want to turn it into this format (tuple of int).

(1, 0, 0, 0, 1, 1, 4)

How can I do it?

1
  • For information : (1,1,0) is a tuple while [1,1,0] is a list, it's similar but they have differences explained here better than I would do Commented May 13, 2022 at 10:38

4 Answers 4

2

You could use map:

>>> nums_list = ['1', '0', '0', '0', '1', '1', '4']
>>> nums_tuple = tuple(map(int, nums_list))
>>> nums_tuple
(1, 0, 0, 0, 1, 1, 4)

Or a comprehension:

>>> nums_tuple = tuple(int(x) for x in nums_list)
>>> nums_tuple
(1, 0, 0, 0, 1, 1, 4)
Sign up to request clarification or add additional context in comments.

Comments

0

You could use list comprehension:

nums_list = ['1', '0', '0', '0', '1', '1', '4']
nums_tuple = tuple([int(x) for x in nums_list])
print(nums_tuple) # (1, 0, 0, 0, 1, 1, 4)

Comments

0

you can use the method below

nums_list = ['1', '0', '0', '0', '1', '1', '4']
nums_tuple = tuple(int(x) for x in nums_list)
print(nums_tuple)

1 Comment

use Code Sample please
-1

change list to numpy array of numbers, and store it my_list = ['1', '0', '0', '0', '1', '1', '4'] my_array = (np.array(my_list,dtype=int))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.