Post

Keep and Drop in the List

DI
DILSHADAB Cloud Author
|
| | 16 Aug 2026
Keep and Drop in the List
### 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

🔒

Join the Discussion

Please sign in or create an account to leave a comment on this article.

💬

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