Unit 4 Session 1 Standard (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
Q: What is the structure of the input?
Q: What is the output?
Q: What should the function return if the input list is empty?
Q: What is the problem in the provided code?
+=
operator, which concatenates each character of the name string individually to the list, resulting in a list of characters instead of a list of full names.Plan the solution with appropriate visualizations and pseudocode.
General Idea: Correct the method used to add names to the list. Instead of concatenating characters, we should append the entire string to the list.
1) Identify the incorrect usage of `+=` in the code.
2) Replace it with the `append()` method to ensure the full name string is added to the list.
3) Test the function with the provided examples to ensure correctness.
**⚠️ Common Mistakes**
- Using `+=` with a list and a string can lead to unintended behavior where each character of the string is added separately to the list.
def extract_nft_names(nft_collection):
nft_names = []
for nft in nft_collection:
nft_names.append(nft["name"])
return nft_names