id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

bryce.hulett@hotmail.com > swap_pairs
prev  |  next  |  chance

Write a function named swap_pairs that accepts a list of strings as a parameter and switches the order of pairs of values in the list. Your function should swap the order of the first two values, then switch the order of the next two, and so on. For example, if the list stores: a = ["a", "bb", "c", "ddd", "f", "ee", "g"] swap_pairs(a) Your function should switch the first pair ("a", "bb"), the second pair ("c", "ddd") and the third pair ("ee", "f"), to yield this list: ["bb", "a", "ddd", "c", "f", "ee", "g"] If there are an odd number of values, the element is not moved. You may assume that the list is not None and that no element of the list is None.


swap_pairs(['four', 'score', 'and', 'seven', 'years', 'ago']) → ['score', 'four', 'seven', 'and', 'ago', 'years']
swap_pairs(['to', 'be', 'or', 'not', 'to', 'be', 'hamlet']) → ['be', 'to', 'not', 'or', 'be', 'to', 'hamlet']
swap_pairs(['I', 'love', 'Python!']) → ['love', 'I', 'Python!']

...Save, Compile, Run (ctrl-enter)

def swap_pairs(lst):

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Python Help

Difficulty: 290 Post-solution available

Copyright Nick Parlante 2017 - privacy