Eddo
Eddo

Reputation: 15

Is there a way to append multiple list with the same value at once - Python

For my school project I'm trying to append a value to multiple lists at once. An example of what I'm trying to do:

list1 = [1, 2, 3]
list2 = [4, 5, 6]

Lets say I'm trying to add a 7 onto the end of both of them. Instead of doing:

list1.append(7)
list2.append(7)

Is there a way I can do this in one line. I tried this but it doesn't work:

(list1, list2).append(7)

Upvotes: 1

Views: 711

Answers (1)

user14143856
user14143856

Reputation:

You can do it using a for loop. See below:

a = [1, 2, 3]
b = [3, 4, 6]
for i in (a,b):
    i.append(7)
    
print(a)
print(b)

This gives us:

[1, 2, 3, 7]
[3, 4, 6, 7]

Upvotes: 1

Related Questions