Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Collections (Retired) Lists Redux Manipulating Lists

Deborah Millians
Deborah Millians
553 Points

Even IT husband is not able to help me create the correct loop in this challenge.

He sat me down to help come up with the pseudo code. But getting it to Python has us stumped.

lists.py
the_list = ["a", 2, 3, 1, False, [1, 2, 3]]

# Your code goes below here

new_list = the_list.pop(3)
the_list.insert(0, new_list)

for item in the_list:
  item = 0

  while True:
    try:
      del the_list(0)
    except:
      break

following through the logic this is what is happening

new_list = the_list.pop(3) returns 1. new_list is not a list, but an int.

the_list.insert(0, new_list) will insert 1 in position 0. the_list will then be [1, "a", 2, 3, False, [1, 2, 3]]

Then you are iterating through the_list.

The 'for item in the_list' will return the first item item == 1 (first item in the_list is 1) you then set item to 0 'item = 0' you then try to delete position 0 in the_list. However since this is a LIST, you cannot use the () to reference items in a list. you can delete items using del the_list[0].

overall the following is happening in the loop

the loop will iterate 6 times (number of elements in the_list) +each loop it will set item to 0 and then it will enter another loop.

  • In the inner loop it will try to delete the_list(0), which is an invalid syntax, given that the_list is a list
  • it will then create an exception, and break out of the inner loop
  • It then cycles to the next iteration of the 'for' loop.

3 Answers

Girri M Palaniyapan
Girri M Palaniyapan
7,829 Points

What's the task specifically demanding Deborah?

Deborah Millians
Deborah Millians
553 Points

The task is to use either the remove or the del prompt to remove the string, the list and the Boolean from the variable. Been trying to create a loop but keep getting syntax errors.

F.Y.I. - I was able to solve the task individually but that seems to be the long way of doing the task.

i wouldn't overcomplicate the solution in this case, the course are still basics and meant to get you use to list comprehensions, loops and certain keywords.

Your intentions are good though in that you are wanting to generalize the solution.

A specific solution would be to just 'del' the specific items in the list (with the relevant indices).

To generalize it, you'd have to look at python's 'type' function.

The pseuo code would be as follows:

for item in list: if type(item) == bool or type(item) == str or type(item) == list then delete item

C H
C H
6,587 Points

I had the same type of question. This is how I went about it, but I still can't get the embedded list of integers deleted automatically.

def clean(alist):
  for item in alist:
    if type(item) is int:
      continue
    else:
      alist.remove(item)