What will be the output of the given code?lis_1= [1,2,3]lis_2= [4,5,6]lis_3=[7,8,9]lis_1.append(lis_3)lis_2.extend(lis_3)print(lis_1,lis_2)
Question
What will be the output of the given code?
lis_1= [1,2,3]
lis_2= [4,5,6]
lis_3=[7,8,9]
lis_1.append(lis_3)
lis_2.extend(lis_3)
print(lis_1,lis_2)
Solution
The output of the given Python code will be:
[1, 2, 3, [7, 8, 9]] [4, 5, 6, 7, 8, 9]
Here's the step by step explanation:
-
Three lists are created:
lis_1
with elements 1, 2, 3;lis_2
with elements 4, 5, 6; andlis_3
with elements 7, 8, 9. -
The
append()
function is used to addlis_3
to the end oflis_1
. This adds the entire listlis_3
as a single element tolis_1
, resulting inlis_1
becoming[1, 2, 3, [7, 8, 9]]
. -
The
extend()
function is used to add the elements oflis_3
to the end oflis_2
. This adds each element oflis_3
individually tolis_2
, resulting inlis_2
becoming[4, 5, 6, 7, 8, 9]
. -
The
print()
function is used to print the contents oflis_1
andlis_2
, resulting in the output:[1, 2, 3, [7, 8, 9]] [4, 5, 6, 7, 8, 9]
.
Similar Questions
What will be the output of the following Python code?x=[[]]*4x[1].append(7)print(x)
What will be the output of the following Python code?A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] B = [[3, 3, 3], [4, 4, 4], [5, 5, 5]] zip(A, B)
l = [1,2,3,4,5,6]l.append([a,b,c,d,e,f,g,h,i,j,k])print(len(l))What will be the output of this code in python
What will the output of the following code be?l = [32, 34, 12, 27, 33]l.append([14, 19])print(len(l))
What is the output of below code snippeta = [ [8, 2] , [4, 1, 2, 2] ]b = a[1][2:]print(b)
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.