LoginSignup
0
2

More than 5 years have passed since last update.

Python > tuple > tuple unpacking

Last updated at Posted at 2017-02-10

@ Introducing Python: Modern Computing in Simple Packages by Bill Lubanovic
(No. 1466 / 12833)

This is sometimes called tuple unpacking.

>>> marx_tuple = ( 'Groucho', 'Chico', 'Harpo' )
>>> a, b, c = marx_tuple
>>> a
'Groucho'
>>> b
'Chico'
>>> c
'Harpo'

tupleのN番目の項目チェックなどに便利なのだろうか。

marx_tuple = ( 'Groucho', 'Chico', 'Harpo' )
print(marx_tuple)

_, item2nd, _ = marx_tuple
print(item2nd)

if 'co' in item2nd:
    print('co inside')
結果
Success time: 0 memory: 23304 signal:0
('Groucho', 'Chico', 'Harpo')
Chico
co inside
0
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2