0
0

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 1 year has passed since last update.

listの展開(アンパック)

Posted at

pythonを学ぶと、*を使った文法が出てきた。
アスタリスクで調べると、ワイルドカード扱いになるため少し時間を要した。

list = [1,2,3,4,5]

print(*list)
# ->1 2 3 4 5

リストに*を与えると、展開する(アンパック)する見たいです。

辞書も展開することができるみたいです。

dict = {'arg1': 'one', 'arg2': 'two', 'arg3': 'three'}
print(*dict)
# ->arg1 arg2 arg3

print関数では`TypeError: 'arg1' is an invalid keyword argument for print()`が出てくるので
関数の引数に与えてあげるといけるぽい

from rich.console import Console
def func_default(arg1=1,arg2=2,arg3=3):
    print('arg1=',arg1)
    print('arg2=',arg2)
    print('arg3=',arg3)
    
  console = Console()
  dict = {'arg1': 'one', 'arg2': 'two', 'arg3': 'three'}
  func_default(**dict)
  #->arg1= one
  #  arg2= two
  #  arg3= three

dictはあんまり使いどころないんじゃないのかな?

参考:
https://note.nkmk.me/python-argument-expand/

0
0
0

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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?