0

for knowledge purpose

Input:

    [[[[[], [], []]], ['trynda'], [[[], [], []]]], ['cait'], [[[[], [], []]], ['veigar'], [[[], [], []]]]]

Output:

    [[[[[], [x], []]], ['trynda'], [[[], [], []]]], ['cait'], [[[[], [], []]], ['veigar'], [[[], [], []]]]]

How do I add x into the 2nd list of the list of the list?

2 Answers 2

3

Just use nested indices (as appropriate for a nested list):

original_list[0][0][0][1].append(x)  # 2nd of 1st of 1st of 1st.

That assumes x is a variable. Otherwise, use a string i.e. 'x'.

Demo:

>>> original_list = [[[[[], [], []]], ['trynda'], [[[], [], []]]], ['cait'], [[[[], [], []]], ['veigar'], [[[], [], []]]]]
>>> original_list[0][0][0][1].append('x')
>>> original_list
[[[[[], ['x'], []]], ['trynda'], [[[], [], []]]], ['cait'], [[[[], [], []]], ['veigar'], [[[], [], []]]]]
          ^
Sign up to request clarification or add additional context in comments.

Comments

1
my_list[0][0][0][1].append(x)

Assuming that x contains a value. You'd want 'x' if it were a string.

3 Comments

Never use 'list' as variable name, because it is python reserved name.
@thefourtheye It is not a builtin function, it is type, if you want to be fully precise =)
It is both a type and a built-in function. In the context of my bad choice of variable name, I would've been overwriting the built-in function docs.python.org/2/library/functions.html#list

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.