How to unpack a single variable tuple in Python3?
Asked Answered
T

4

14

I have a tuple-

('[email protected]',).

I want to unpack it to get '[email protected]'.

How can I do so?

I am new to Python so please excuse.

Trawl answered 1/7, 2020 at 7:26 Comment(3)
If it will always have only one element u can use indices to get the element. t[0] where t is ur tupleCaernarvonshire
What are you planning on doing with the content? is it always single size tuple?Limbo
If you are this new, you are better off trying to follow a tutorial than asking every question you think of on Stack Overflow. The main Python website provides a tutorial that will cover quite a bit of ground: docs.python.org/3/tutorial/index.html Specifically tuples are introduced here: docs.python.org/3/tutorial/…Hyperspace
L
4
tu = ('[email protected]',)

str = tu[0]

print(str) #will return '[email protected]'

A tuple is a sequence type, which means the elements can be accessed by their indices.

Lindalindahl answered 1/7, 2020 at 7:34 Comment(0)
N
35

The full syntax for unpacking use the syntax for tuple literal so you can use

tu = ('[email protected]',)
(var,) = tu

The following simplified syntax is allowed

var, = tu
Nicotinism answered 1/7, 2020 at 7:59 Comment(1)
Even though the question seems like an easy one, but actually "unpacking" is not the same as just picking the first index, so I would choose this as the actual answer as it answers what the title requested. (Even though OP might not have intended it)Shepard
R
7

The prettiest way to get the first element and the last element of an iterable object like tuple, list, etc. would be to use the * feature not same as the * operator.

my_tup = ('a', 'b', 'c',)

# Last element
*other_els, last_el = my_tup

# First element
first_el, *other_els = my_tup

# You can always do index slicing similar to lists, eg [:-1], [-1] and [0], [1:]

# Cool part is since * is not greedy (meaning zero or infinite matches work) similar to regex's *. 
# This will result in no exceptions if you have only 1 element in the tuple.
my_tup = ('a',)

# This still works
# Last element
*other_els, last_el = my_tup

# First element
first_el, *other_els = my_tup

# other_els is [] here

Relegate answered 27/5, 2021 at 7:32 Comment(2)
('a') is not a tuple, it resolves to 'a'. A tuple with one element would be ('a',). But cool to know, *other_els, last_el = 'a' is working also.Camaraderie
I forgot to put a , at the end. Please don't use ('a') even if you think that you are getting the right result, it's not right. Since strings are sliceable the *rest, target list explosion syntax works without errors. But if you use a longer string like ('abc') this will break resulting in rest = ['a', 'b'] and target = 'c'Relegate
L
4
tu = ('[email protected]',)

str = tu[0]

print(str) #will return '[email protected]'

A tuple is a sequence type, which means the elements can be accessed by their indices.

Lindalindahl answered 1/7, 2020 at 7:34 Comment(0)
S
2

A tuple is just like a list but static, so just do :

('[email protected]',)[0]
Sideman answered 1/7, 2020 at 7:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.