Python Exercise — Keep or Drop Cities from a List
Create a list that contains different values, including another list nested inside the main list.
Then create two methods:
- Keep Cities
Create an empty list and ask the user how many items they want to keep from the beginning of the main list, based on their choice. Add the selected items to the new list and display the result.
2. Drop Cities
Ask the user how many items they want to drop from the beginning of the main list. Display only the remaining items in the list.
The number entered by the user determines how many items will be included in the resulting list.
Example
City list: [55, 'London', 'Berlin', ['a', 'b', 3, 4],
'New York', 'Paris', 'Rome', 'Oslo']
Enter the number of items you want to keep from the list: 3
Keep first items: [55, 'London', 'Berlin']
===========================================
City list: [55, 'London', 'Berlin', ['a', 'b', 3, 4],
'New York', 'Paris', 'Rome', 'Oslo']
Enter the number of items you want to drop from the list: 5
Remaining items: ['Paris', 'Rome', 'Oslo']
Note: I changed "New Yourk" to "New York", and used "items" rather than "cities" because your list contains 55 and a nested list (['a', 'b', 3, 4]), which are not cities.
1. Architectural Overview & Systems Context
In modern production environments, implementing Keep and Drop in the List requires a disciplined understanding of distributed systems boundaries, deterministic resource lifecycles, and operational observability. Within the Post engineering stack, teams frequently encounter subtle failure modes when moving from local prototypes to high-concurrency Linux production clusters—ranging from connection pool exhaustion and unbounded memory growth to non-idempotent retry storms.
To engineer a resilient system around Keep and Drop in the List , backend architects separate concerns into three distinct operational planes:
- The Ingress & Validation Plane: Enforcing strict schema contracts, request payload size ceilings, rate limiting, and cryptographic authentication before any compute-heavy logic executes.
- The Core Execution & Domain Plane: Isolating business invariants and algorithmic transformations from transport-layer protocols (HTTP/gRPC/WebSockets) so domain rules remain deterministic and unit-testable in under 10 milliseconds.
- The Persistence & Telemetry Plane: Managing transactional atomicity (ACID guarantees or Write-Ahead Logging), structured JSON logging with correlation IDs, and Prometheus/OpenTelemetry latency histograms (`p50`, `p95`, `p99`).
2. Production Implementation Standards & Failure Isolation
When deploying workloads related to Keep and Drop in the List on Linux cloud infrastructure (such as DigitalOcean Droplets, AWS EC2, or Kubernetes worker nodes), engineers must account for hardware-level and kernel-level constraints:
- Bounded Timeouts & Backpressure: Never issue network, IPC, or database calls without explicit connect and read timeouts. Unbounded sockets tie up Gunicorn/Uvicorn worker processes and trigger cascading
502 Bad Gateway outages under upstream degradation.
- Memory Profile & Garbage Collection Discipline: Stream large datasets lazily using Python generators (
yield / yield from) or chunked cursors rather than materializing multi-megabyte collections into heap memory at once.
- Graceful Shutdown & Signal Handling: Ensure worker daemons trap
SIGTERM and SIGINT signals cleanly, draining active in-flight transactions and closing file descriptors before process termination.
- Idempotency & Safe Retries: Pair exponential backoff and jitter with deterministic idempotency keys so transient network blips never cause duplicate state mutations.
3. Security, Hardening & Pre-Deployment Checklist
Before promoting any change involving Keep and Drop in the List through your CI/CD pipeline into production, verify the following engineering safeguards:
- Zero Hardcoded Secrets: Inject all API keys, TLS certificates, and database DSNs via environment variables or dedicated secret managers with strict
0600 file permissions.
- Principle of Least Privilege: Run application containers and systemd services under dedicated non-root POSIX user accounts (`uid != 0`) with read-only root filesystems where possible.
- Automated Regression Verification: Validate both happy-path execution and boundary error conditions (timeouts, malformed inputs, lock contention) in automated `pytest` suites prior to deployment.
- Live Telemetry & Alerting: Confirm that error rates, CPU saturation, and memory RSS metrics are actively scraped and routed to on-call alerting channels.