![]() |
|
[Tut] How to Swap List Elements in Python? - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] How to Swap List Elements in Python? (/thread-99109.html) |
[Tut] How to Swap List Elements in Python? - xSicKxBot - 03-31-2022 How to Swap List Elements in Python? Problem Formulation Given a list of size Code: nCode: i,j < nSwap the element at index Code: iCode: jCode: list[i]Code: jCode: list[j]Code: iExamples:
Method 1: Multiple Assignment To swap two list elements by index Code: iCode: jCode: lst[i], lst[j] = lst[j], lst[i]Code: iCode: jCode: lst = ['alice', 'bob', 'carl']
i, j = 0, 2 # Swap index i=0 with index j=2
lst[i], lst[j] = lst[j], lst[i] print(lst)
# ['carl', 'bob', 'alice']The highlighted line works as follows:
To help you better understand this code snippet, I’ve recorded a quick video that shows you how the generalization of multiple assignment, i.e., slice assignment, works as a Python One-Liner: Method 2: Swap Two Elements by Value Using indexof() Let’s quickly discuss a variant of this problem whereby you want to swap two elements but you don’t know their indices yet. To swap two list elements Code: xCode: yCode: list.index(x)Code: list.index(y)Code: iCode: jCode: lst[i], lst[j] = lst[j], lst[i]The latter part, i.e., swapping the list elements, remains the same. The main difference is highlighted in the following code snippet: Code: lst = ['alice', 'bob', 'carl']
x, y = 'alice', 'carl' # Get indices i and j associated with elements x and y
i, j = lst.index(x), lst.index(y) # Swap element at index i with element at index j
lst[i], lst[j] = lst[j], lst[i] print(lst)
# ['carl', 'bob', 'alice']Do you need a quick refresher on the Code: list.index() Background: The Code: list.index(value)Code: valueCode: listCode: startCode: stopCode: ValueErrorFeel free to also watch the following quick explainer video: Python One-Liners Book: Master the Single Line First! Python programmers will improve their computer science skills with these useful one-liners. ![]() Python One-Linerswill teach you how to read and write “one-liners”: concise statements of useful functionality packed into a single line of code. You’ll learn how to systematically unpack and understand any line of Python code, and write eloquent, powerfully compressed Python like an expert. The book’s five chapters cover (1) tips and tricks, (2) regular expressions, (3) machine learning, (4) core data science topics, and (5) useful algorithms. Detailed explanations of one-liners introduce key computer science concepts and boost your coding and analytical skills. You’ll learn about advanced Python features such as list comprehension, slicing, lambda functions, regular expressions, map and reduce functions, and slice assignments. You’ll also learn how to:
By the end of the book, you’ll know how to write Python at its most refined, and create concise, beautiful pieces of “Python art” in merely a single line. Get your Python One-Liners on Amazon!! |