2

How to replace elements in a particular position nested list python 3? i want to insert elements in a[1] position. The inserted elements are in list b.

a = [['04\01\1997','alphanum4569874','22','4.0'],['07\01\1997','Anee_69213654','23','2.0']]

b = ['alphanum1','alphanum2']

for idx,item in enumerate(b):
    for i in b:
        a[1].append(i)

print(a)

Expected Output:

[['04\01\1997','alphanum1','22','4.0'['07\01\1997','alphanum1','23','2.0']]

Current Output:

[['04\x01\x01997', 'alphanum4569874', '22', '4.0'], ['07\x01\x01997', 'Anee_69213654', '23', '2.0', 'alphanum1', 'alphanum2', 'alphanum1', 'alphanum2']]
1
  • I don't think that's the expected output, since the number of brackets doesn't match - did you mean [['04\01\1997','alphanum1','22','4.0'], ['07\01\1997','alphanum2','23','2.0']]? So, basically, you're looking to replace the 2nd element of the sublists of the first list with elements of the second list? Commented Jun 17, 2019 at 3:36

5 Answers 5

2

I'm assuming your list b will always have the same number of elements as there are lists in a and that you always want to replace (not insert) the second element.

This works:

for ax, bx in zip(a, b):
    ax[1] = bx

First zip pairs up every element from a with the corresponding element from b, the for loop then gives you each pair one at a time as ax and bx. Since ax is a list, it's actually the list in a (not a copy) and with a[1] = bx you're just overwriting the second element in the list with bx.

Another way of doing it would be with a list comprehension:

new_a = [[ax[0], bx, *ax[2:]] for ax, bx in zip(a, b)]

This has the advantage of not modifying the original a, but gives you a new list with the replacements.

What happens here is that it still uses zip for the pairing, but instead of replacing ax[1], it makes a new list with the first element of ax, followed by bx and then followed by the rest of ax from the third element onwards. Notice the * in there - that 'explodes' the list into its separate elements, so they can be added to the new list.

You also asked about avoiding zip, although I think this is a worse solution:

for i in range(len(a)):
    a[i][1] = b[i]

What this does is let i run from 0 to the length of a minus one, replacing the 2nd element of each element of a one at a time with the matching element from b, by using i to index both a and b.

Sign up to request clarification or add additional context in comments.

2 Comments

thank ypu so much. i don't know the usage of zip. so any alternative for this solution other than using zip? i want solution in simplify
I'll add an example not using zip, but from my perspective it would be more complicated than zip - zip is a very useful function to know, since you don't have to keep track of your position in both the lists.
2

You can use zip:

a = [['04\01\1997','alphanum4569874','22','4.0'],['07\01\1997','Anee_69213654','23','2.0']]
b = ['alphanum1','alphanum2']
result = [[c, d, *j] for d, [c, _, *j] in zip(b, a)]

Output:

[['04\x01\x01997', 'alphanum1', '22', '4.0'], ['07\x01\x01997', 'alphanum2', '23', '2.0']]

Comments

1

this code works

a = [['04\01\1997','alphanum4569874','22','4.0'],['07\01\1997','Anee_69213654','23','2.0']]

b = ['alphanum1','alphanum2']
c=0
for i in a:
    i[1]=b[c]
    c+=1
print(a)

output

[['04\x01\x01997', 'alphanum1', '22', '4.0'], ['07\x01\x01997', 'alphanum2', '23', '2.0']]

3 Comments

thank you so much but its not replacing its just appending
in question it was give to insert this is my output [['04\x01\x01997', 'alphanum1', 'alphanum4569874', '22', '4.0'], ['07\x01\x01997', 'alphanum2', 'Anee_69213654', '23', '2.0']]
i want [['04\x01\x01997', 'alphanum1', '22', '4.0'], ['07\x01\x01997', 'alphanum2', '23', '2.0']]
0

Or try:

print([[x[0], y, *x[2:]] for x, y in zip(a, b)])

Output:

[['04\x01\x01997', 'alphanum1', '22', '4.0'], ['07\x01\x01997', 'alphanum2', '23', '2.0']]

2 Comments

thank ypu so much. i don't know the usage of zip. so any alternative for this solution other than using zip? i want solution in simplify
@suba You should learn zip instead, here is the documentation: python-reference.readthedocs.io/en/latest/docs/functions/…
-1

I think what you want is this:

 for item in a:
    for i in b:
       item[1].append(i)

 print (a)

1 Comment

'str' object has no attribute 'append' i m getting this erro how to resolve

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.