PGoCareerGoCareer prep tools
Home
LoginSign up
  • Java
  • Python
  • AI
  • React
  • Angular
  • PHP
  • Node.js
  • SQL
  • DSA
  • HTML
  • CSS
  • JS
  • Spring
  • ML
  • MongoDB

Python · Theory

dict.fromkeys()

← All stacks

Theory

260/268

dict.fromkeys()

dict.fromkeys() works on a dictionary — labelled data like marks["Asha"] = 90. Keys map to values. Loop with .items() when you need both.

dict.fromkeys() — output — {'Asha': 0, 'Ravi': 0}.

Trap for dict.fromkeys() — writing d[missing] in a viva. Prefer get when the key might not exist.

Diagram
{ "Asha": 90, "Ravi": 85 }
         │           │
        key        value
Exam tip

get vs [] — one sentence + tiny dict of marks.

Example

# dict.fromkeys()
print(dict.fromkeys(["Asha", "Ravi"], 0))

dict.fromkeys() — output: {'Asha': 0, 'Ravi': 0}.

Short notes

  • Defdict.fromkeys() works on a dictionary.
  • Ruledict.fromkeys() — get when key may miss.
  • Rememberdict.fromkeys() — items() → key and value.
  • Trapdict.fromkeys() — d[missing] → KeyError.

Questions

1

Explain dict.fromkeys() as if you are teaching a junior — definition, then one tiny script.

2

What does the example print, and why?

3

What mistake do freshers make with dict.fromkeys()?

Previous← dict.clear()Nextdict.keys() →
P

GoCareerGo

Utilities · Preparation Hub · Resume · CV · Tools — one workspace.

Workspace

DashboardProfilePreparation HubResume builderCV builderCareer planning

PDF Tools

Merge PDFSplit PDFCompress PDFImage to PDFAll toolsJobs

Image & QR

Compress ImageResize ImageQR ScannerQR GeneratorBlogIT interview prep

Company

FAQFeedbackContactPrivacyTermsSitemap

© 2026 GoCareerGo. Keep moving forward.