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
Games Programmer Interview questions
Games Programmer…
There is no other answer that is also…
Interview questions to ask teachers
Interview questions…
Back-to-school content is usually focused…
NHS Nursing Assistant interview questions
NHS Nursing Assistant…
The National Health Service offers professional…
New Grad nurse interview questions
New Grad nurse…
Dear nursing applicant: It’s tough getting…
What do you mean by Aptitude?
What do you mean…
Identifier: youi00clev Title: You and…
Search
Featured posts
  • Financial Analyst technical Interview questions
  • Technical interview questions for Mechanical design engineers
  • Software developer technical Interview questions
  • Technical Interview questions for Programmers
  • SAP Basis technical interview questions
  • Basic technical interview questions
  • Interview questions technical
  • QA technical interview questions and answers
  • Amazon technical interview questions
Copyright © 2024 l www.floydfairnessfund.org. All rights reserved.