我们可以使用for循环遍历列表和字符串,然后使用索引或切片操作获取第一个元素。
示例代码:
nested_list = [['apple', 'orange'], ['banana', 'grape'], ['watermelon']]
for l in nested_list: print(l[0])
for l in nested_list: print(l[:1])
上一篇:遍历嵌套字典,并将键的值为True的情况加入打印语句中。
下一篇:遍历器超出列表末尾时自增