1

I want to slice certain parts of a string inside a list into another list, for example consider their is the list data:

data = ["xbox 360 | 13000 | new","playstation 4 | 30000 | new","playstation 3 | 30000 | old","playstation 2 | 30000 | old"]

I want to slice each component into three,

product = ["xbox 360","playstation 4","playstation 3","playstation 2"]
cost = ["13000","30000","30000","30000"]
condition = ["new","new","old","old"]

please help me

3
  • What did you try? Commented Jul 8, 2018 at 5:45
  • Did you try a for loop? Commented Jul 8, 2018 at 5:46
  • @surya, you can also use list comprehension with reduce() method to accomplish your task just with the use of 1 line statement products, cost, condition = reduce(lambda s1, s2: [s1[index] + [item.strip()] for index, item in enumerate(s2.split('|'))], data,[[], [], []]). Commented Jul 8, 2018 at 6:35

4 Answers 4

4

The following uses the common zip(*...) transpositioning pattern while splitting the strings on an appropriate separator:

>>> prd, cst, cnd = zip(*(s.split(' | ') for s in data))
>>> prd
('xbox 360', 'playstation 4', 'playstation 3', 'playstation 2')
>>> cst
('13000', '30000', '30000', '30000')
>>> cnd
('new', 'new', 'old', 'old')
Sign up to request clarification or add additional context in comments.

1 Comment

map(list,zip(*(s.split(' | ') for s in data)))?
1

@surya, you can try any one of the below 2 approaches. The 1st one is very short which will give your all 3 lists just with an execution of one line statement. I have used he concept of list comprehension and reduce() function.

Use split() to get the words separated with | for each of the list items.

Use strip() to remove leading/trailing whitespaces.

1st way (one line statement)

Just use product, cost, condition = reduce(lambda s1, s2: [s1[index] + [item.strip()] for index, item in enumerate(s2.split('|'))], data,[[], [], []]); and it will give your lists.

>>> data = ["xbox 360 | 13000 | new","playstation 4 | 30000 | new","playstation 3 | 30000 | old","playstation 2 | 30000 | old"]
>>>
>>> product, cost, condition = reduce(lambda s1, s2: [s1[index] + [item.strip()] for index, item in enumerate(s2.split('|'))], data,[[], [], []]);
>>>
>>> product
['xbox 360', 'playstation 4', 'playstation 3', 'playstation 2']
>>>
>>> cost
['13000', '30000', '30000', '30000']
>>>
>>> condition
['new', 'new', 'old', 'old']
>>>

2nd way

>>> data = ["xbox 360 | 13000 | new","playstation 4 | 30000 | new","playstation 3 | 30000 | old","playstation 2 | 30000 | old"]
>>>
>>> product = []
>>> cost = []
>>> condition = []
>>>
>>> for s in data:
...     l = [item.strip() for item in s.split("|")]
...     product.append(l[0])
...     cost.append(l[1])
...     condition.append(l[2])
...
>>> product
['xbox 360', 'playstation 4', 'playstation 3', 'playstation 2']
>>>
>>> cost
['13000', '30000', '30000', '30000']
>>>
>>> condition
['new', 'new', 'old', 'old']
>>>
>>>

Comments

1

You could loop through your list of data and then split each element. Then you can add that data to the product, cost and condition lists by using .append():

data = ["xbox 360 | 13000 | new","playstation 4 | 30000 | new","playstation 3 | 30000 | old","playstation 2 | 30000 | old"]
product = []
cost = []
condition = []
for string in data:
    strSplit = string.split(" | ")
    product.append(strSplit[0])
    cost.append(strSplit[1])
    condition.append(strSplit[2])

print(product)
print(cost)
print(condition)

Result of the following code:

['xbox 360', 'playstation 4', 'playstation 3', 'playstation 2']
['13000', '30000', '30000', '30000']
['new', 'new', 'old', 'old']

1 Comment

Ah yes, I just used arrays as the question had the it as a tag. However you are definitely correct correct, I’ll update my answer, thank you :)
0
data = ["xbox 360 | 13000 | new","playstation 4 | 30000 | new","playstation 3 | 30000 | old","playstation 2 | 30000 | old"]
product = [y.split("|")[0].strip() for y in data]
cost = [y.split("|")[1].strip() for y in data]
condition = [y.split("|")[2].strip() for y in data]
print(product)
print(cost)
print(condition)

Output

['xbox 360', 'playstation 4', 'playstation 3', 'playstation 2']
['13000', '30000', '30000', '30000']
['new', 'new', 'old', 'old']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.