python – Append a tuple to a list – whats the difference between two ways?

python – Append a tuple to a list – whats the difference between two ways?

The tuple function takes only one argument which has to be an iterable

tuple([iterable])

Return a tuple whose items are the same and in the same order as iterable‘s items.

Try making 3,4 an iterable by either using [3,4] (a list) or (3,4) (a tuple)

For example

a_list.append(tuple((3, 4)))

will work

Because tuple(3, 4) is not the correct syntax to create a tuple. The correct syntax is –

tuple([3, 4])

or

(3, 4)

You can see it from here – https://docs.python.org/2/library/functions.html#tuple

python – Append a tuple to a list – whats the difference between two ways?

I believe tuple() takes a list as an argument
For example,

tuple([1,2,3]) # returns (1,2,3)

see what happens if you wrap your array with brackets

Leave a Reply

Your email address will not be published. Required fields are marked *