4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

pythonのtupleをイテレーションするときの話

Last updated at Posted at 2016-07-14

こんな風に書けることを知りました。使ったのは2.7.11だけど3も同じかと。

items = [1, 2, 3, 4, 5]
pairs = [('aaa', 100), ('bbb', 200) ,('ccc', 300), ('ddd', 400), ('fff', 500)]

for key, value in pairs:
    print key, value
#>>>
#aaa 100
#bbb 200
#ccc 300
#ddd 400
#fff 500

for item, pair in zip(items, pairs):
    print item, pair[0], pair[1]    
#>>>
#1 aaa 100
#2 bbb 200
#3 ccc 300
#4 ddd 400
#5 fff 500

for item, (key, value) in zip(items, pairs):
    print item, key, value
#>>>
#1 aaa 100
#2 bbb 200
#3 ccc 300
#4 ddd 400
#5 fff 500

4
5
1

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
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?