Backend

Keep and Drop in the List

DI
DILSHADAB Cloud Author
|
| | 16 Aug 2026
f6d956b5f6f06387.jpeg preview.py
RAW COPY
Keep and Drop in the List CODE
Teach Cloud UTF-8 Python Ln 1, Col 1
### 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**: 1. **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.

0 Comments

Leave a comment

0 / 2000
πŸ’¬

No comments yet. Be the first to share your thoughts!