简体   繁体   中英

Python - is it possible to insert() into a list inside of a list?

In Python is it possible to insert a value inside a list that's inside a list?

for example:

    List = [['Name',[1, 4, 6]],
    ['Another Name', [1,2,5]]]

I've tried to use:

    List.insert([0][1], 'another value')

but it doesn't like that, is there another way to manipulate lists inside lists?

Definitely possible:

>>> List = [['Name',[1, 4, 6]],
...     ['Another Name', [1,2,5]]]
>>> List[0].insert(1,"Another Value")
>>> List
[['Name', 'Another Value', [1, 4, 6]], ['Another Name', [1, 2, 5]]]

You just need to subscript the "outer" list to get a reference to the "inner" list that you want to insert into.

We could break the above code into the following steps:

inner = List[0]
inner.insert(1,'Another Value')

If that makes it more clear to you what I actually did there ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM