Compact way to assign values by slicing list in Python

后端 未结 5 1198
悲哀的现实
悲哀的现实 2020-12-01 07:04

I have the following list

bar = [\'a\',\'b\',\'c\',\'x\',\'y\',\'z\']

What I want to do is to assign 1st, 4th and 5th values of bar

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-01 08:10

    Assuming that your indices are neither dynamic nor too large, I'd go with

    bar = ['a','b','c','x','y','z']
    v1, _, _, v2, v3, _ = bar
    

提交回复
热议问题