From List<T> to list
Lesson 12 of 48 in Coddy's Python for C# Developers course.
Update and inspect Python lists without losing their references.
C# reference excerpt
Executable statements use C# top-level program style; type-only examples are declarations. System names assume the usual using System directive.
var values = new System.Collections.Generic.List<int> { 2, 4 };
values.Add(6);
Console.WriteLine(values[values.Count - 1]);Python
values = [2, 4]
values.append(6)
print(values[-1])What changes
A Python list is a resizable sequence, closer to List<T> than a fixed-length C# array. It can hold mixed types, though consistent element types make code clearer. Indexes start at zero; -1 selects the last element. values[:2] creates a new shallow list containing up to two elements. append mutates and returns None; pop(index) removes and returns the item.
Transfer mistake
C# List<T>.RemoveAt returns void; Python pop gives back the removed element. Do not assign the result of append to your list name.
Your task
Four lines contain integers in [-20, 20]. Form a list from the first three, append the fourth, then remove index zero with pop(0). Output the removed value, the final item, and a slice of the first two remaining items, each on its own line. Use ordinary Python list formatting for the slice. No loop is needed.
Challenge
BeginnerFour lines contain integers in [-20, 20]. Form a list from the first three, append the fourth, then remove index zero with pop(0). Output the removed value, the final item, and a slice of the first two remaining items, each on its own line. Use ordinary Python list formatting for the slice. No loop is needed.
Editable implementation files: main.py. The locked driver.py is the supplied launcher or input/output harness and must remain unchanged.
Use the supplied input contract without prompts. Print only the required lines, each ending in a newline.
Try it yourself
# Starts the program in main.py. Edit main.py to solve the challenge.
from runpy import run_module
run_module("main", run_name="__main__")
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Python for C# Developers
1C# Foundations in Python
Run the Python WorkspaceOutput, Strings, and CommentsTyped Locals to Python NamesDivision and RemaindersIndentation Instead of BracesRecap: Port a Receipt4Loops and Transformations
From foreach to forIndexes and Dictionary ItemsSentinels and Loop ControlFrom LINQ to ComprehensionsRecap: Stable Ranking2None and Decisions
Nullable Values and NoneEquality, Identity, and TypesTruth Tests and FallbacksBranch PriorityRecap: Eligibility Rules3Collections and References
From List<T> to listTuples and UnpackingDictionary Keys and ValuesSets, Aliases, and CopiesRecap: Stock Lookup6Reliable Python Programs
Input and Exact OutputExceptions and ValidationImports and the Main GuardType Hints and Runtime TypesRecap: Validate a Batch9Project: Order Processor
Project 1: Order ModelProject 2: Pricing PoliciesPractice on your own: Online Python compiler