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

Move the 1 to position 0. You can do this in one step with .pop() and .insert().

helppppppppppppppppppppppppppp

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

# Your code goes below here

2 Answers

No need for the temp variable. It can be done with pop() and .insert() on a single line.

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

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

From the Lists Redux manipulating lists Challenge:

Move the 1 to position 0. You can do this in one step with .pop() and .insert().

Given the list:

the_list = ["a", 2, 3, 1, False, [1, 2, 3]]

The "1" is the 4-th item in the list and can be reference as list[3] (using 0-based counting).

Use the pop() method to "pop" an item off of the list. Assign this to a temp variable:

temp = the_list.pop(3)

Use the insert() method to "insert" an item into a list at a specific position. Inserting temp into the_list at position 0::

the_list.insert(0, temp)

All together, running in ipython, it would look like:

In [84]: the_list = ["a", 2, 3, 1, False, [1, 2, 3]]

In [85]: the_list[3]
Out[85]: 1

In [86]: temp = the_list.pop(3)

In [87]: temp
Out[87]: 1

In [88]: the_list
Out[88]: ['a', 2, 3, False, [1, 2, 3]]

In [89]: the_list.insert(0, temp)

In [90]: the_list
Out[90]: [1, 'a', 2, 3, False, [1, 2, 3]]