![]() |
|
[Tut] Python Join List Slice - 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] Python Join List Slice (/thread-95675.html) |
[Tut] Python Join List Slice - xSicKxBot - 06-14-2020 Python Join List Slice To join and replace all strings in a list [b] Code: lstCode: lst[1:5] = [''.join(lst[1:5])]Problem: Given a list of strings. How to join all strings in a given list slice and replace the slice with the joined string? Example:You start with the following list: Code: lst = ['i', 'l', 'o', 'v', 'e', 'u']You want to join the slice Code: ['l', 'o', 'v', 'e']Code: # Output: ['i', 'love', 'u']You can get a quick overview of all three methods in the following interactive Python shell. If you’re short on time—method 1 using slice assignment is the most Pythonic solution! https://trinket.io/embed/python/a7c4fa6ef4 Exercise: Modify the code so that only elements with start index 2 and end index 4 (included) are replaced by the joined string! Method 1: Slice Assignment Slice assignment is a little-used, beautiful Python feature to replace a slice with another sequence. Select the slice you want to replaceon the left and the values to replace it on the right side of the equation. Here’s how you can join and replace all strings in a list slice (between indices 1 and 5) in a single line of Python code: Code: # Method 1: Slice Assignments
lst = ['i', 'l', 'o', 'v', 'e', 'u']
lst[1:5] = [''.join(lst[1:5])]
print(lst)
# ['i', 'love', 'u']The one-liner Code: lst[1:5] = [''.join(lst[1:5])]
If you love the power of Python one-liners, check out my new book with the same name “Python One-Liners” on Amazon (published in 2020 with San Francisco’s high-quality NoStarch publishing house). Method 2: List Concatenation + Slicing + Join A simpler but quite readable method is to use simple list concatenation. Read more on my Finxter blog tutorial to master all different ways to concatenate lists in Python. Code: # Method 2: List Concatenation + Join
lst = ['i', 'l', 'o', 'v', 'e', 'u']
lst = lst[:1] + [''.join(lst[1:5])] + lst[5:]
print(lst)
# ['i', 'love', 'u']This approach uses three slicing calls to select (or create) three lists. Then, it glues them together using the Code: +Method 3: Naive This method is what a non-Python coder (maybe coming from Java or C++) would use. It’s NOT the recommended way though. Code: # Method 3: Naive
lst = ['i', 'l', 'o', 'v', 'e', 'u']
new = ''
for i in range(1,5): new += lst[i]
lst = lst[:1] + [new] + lst[5:]
print(lst)
# ['i', 'love', 'u']Instead of selecting the slice of strings to be joined using slicing, the coder creates a string variable Code: newWhere to Go From Here? Enough theory, let’s get some practice! To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs? Practice projects is how you sharpen your saw in coding! Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people? Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner. Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home. Join the free webinar now! |