15 Essential Python Interview

Python technical interview questions


[, , , , ] [[10], [10], [10], [10], [10]] [[10, 20], [10, 20], [10, 20], [10, 20], [10, 20]] [[10, 20], [10, 20], [10, 20], [10, 20], [10, 20], 30]

Here’s why:

The first line of output is presumably intuitive and easy to understand; i.e., list = [ [ ] ] * 5 simply creates a list of 5 lists.

However, the key thing to understand here is that the statement list = [ [ ] ] * 5 does NOT create a list containing 5 distinct lists; rather, it creates a a list of 5 references to the same list. With this understanding, we can better understand the rest of the output.

list[0].append(10) appends 10 to the first list. But since all 5 lists refer to the same list, the output is: [[10], [10], [10], [10], [10]].

Similarly, list[1].append(20) appends 20 to the second list. But again, since all 5 lists refer to the same list, the output is now: [[10, 20], [10, 20], [10, 20], [10, 20], [10, 20]].

In contrast, list.append(30) is appending an entirely new element to the “outer” list, which therefore yields the output: [[10, 20], [10, 20], [10, 20], [10, 20], [10, 20], 30].



Share this article





Related Posts



Latest Posts
Customer service supervisor interview questions and answers
Customer service…
ANNALS OF ESPIONAGE THE TRAITOR The case…
Accounting Behavioral interview questions and Answers
Accounting Behavioral…
31 Commonly Used Big 4 Accounting Firm…
Example of numerical Reasoning
Example of numerical…
In a numerical reasoning test, you are…
Sample Aptitude questions with Answers
Sample Aptitude…
General Science tests the ability to…
Salesforce Applicant tracking
Salesforce Applicant…
Recently, I’ve spoken with a few different…
Search
Featured posts
  • Technical interview questions for Mechanical design engineers
  • Oracle SQL developer Interview questions
  • Quicken Loans internship interview questions
  • Network Architect Interview questions
  • Computer Architecture Interview questions
  • Nursing Assistant interview questions
  • Nursing program Interview questions
  • Microsoft program Manager Interview questions
  • Legal assistant interview questions
Copyright © 2019 l www.floydfairnessfund.org. All rights reserved.