Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Split Python list field into two and leave them in place

How would you split a field in Python and then leave the two fields side by side in the list? Is there a command to use asides from .split()? Or would it make more sense to write a function that does this? How would it be done across multiple lists

Performance is not a major concern. Below is an example of what I’m trying to accomplish.

Edit: I don’t mind using split(), I just wanted to know how something like this could be implemented.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

List before formatting

['Intel', 'Core', 'i5-4210Y', '@', '1.50GHz', '(20%)', '998', 'N']

List after formatting

['Intel', 'Core', 'i5', '4210Y', '@', '1.50GHz', '(20%)', '998', 'N']

>Solution :

Here is one way to do so, using list comprehension and split():

data = ['Intel', 'Core', 'i5-4210Y', '@', '1.50GHz', '(20%)', '998', 'N']

new_data = [element for item in data for element in item.split("-")]
print(new_data)  # ['Intel', 'Core', 'i5', '4210Y', '@', '1.50GHz', '(20%)', '998', 'N']

The equivalent with for loops would be:

new_data = []
for item in data:
    for element in item.split("-"):
        new_data.append(element)
print(new_data)  # ['Intel', 'Core', 'i5', '4210Y', '@', '1.50GHz', '(20%)', '998', 'N']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading