3

Is it possible to slice a string in a list using a separator?

When I use sep = ":" and try to slice I get an error saying that slicing is only possible with number indexing. I'd like to slice each string on the : separator.

['Brandon:5', 'Patrick:18.9', 'Brandon:xyz', 'Jack:', 'Sarah:825', 'Jack:45', 'Brandon:10', 'James:3.25', 'James:125.62', 'Sarah:2.43', 'Brandon:100.5']

text2 =  ['Brandon:5', 'Patrick:18.9', 'Brandon:xyz', 'Jack:', 'Sarah:825', 'Jack:45', 'Brandon:10', 'James:3.25', 'James:125.62', 'Sarah:2.43', 'Brandon:100.5']

sep = ':'

text3 = [w[:sep] for w in text2]

Output:

TypeError: slice indices must be integers or None or have an __index__ method
Red
  • 26,798
  • 7
  • 36
  • 58
maverick
  • 53
  • 8

2 Answers2

3

You can split each string in a list comprehension with str.split(). This will give you a list of lists. You can than zip this and unpack it into separate lists:

l = ['Brandon:5', 'Patrick:18.9', 'Brandon:xyz', 'Jack:', 'Sarah:825', 'Jack:45', 'Brandon:10', 'James:3.25', 'James:125.62', 'Sarah:2.43', 'Brandon:100.5']

names, data = zip(*(s.split(':') for s in l))

names will be:

('Brandon',
 'Patrick',
 'Brandon',
 'Jack',
 'Sarah',
 'Jack',
 'Brandon',
 'James',
 'James',
 'Sarah',
 'Brandon')

and data will be:

('5', '18.9', 'xyz', '', '825', '45', '10', '3.25', '125.62', '2.43', '100.5')

Red
  • 26,798
  • 7
  • 36
  • 58
Mark
  • 90,562
  • 7
  • 108
  • 148
1

UPDATE

The reason your code throws an error is because you'll need to pass the index of the character as the slices, and the characters themselves:

text2 =  ['Brandon:5', 'Patrick:18.9', 'Brandon:xyz', 'Jack:', 'Sarah:825', 'Jack:45', 'Brandon:10', 'James:3.25', 'James:125.62', 'Sarah:2.43', 'Brandon:100.5']

sep = ':'

text3 = [w[:w.index(sep)] for w in text2]
text4 = [w[w.index(sep):] for w in text2]

Mark Meyer's solution is optimal, but not very beginner friendly.

Here is a step by step procedure:

  1. We have the this list:
['Brandon:5', 'Patrick:18.9', 'Brandon:xyz', 'Jack:', 'Sarah:825', 'Jack:45', 'Brandon:10', 'James:3.25', 'James:125.62', 'Sarah:2.43', 'Brandon:100.5']

Let's assign it to a variable so that we can access it later in the program:

lst = ['Brandon:5', 'Patrick:18.9', 'Brandon:xyz', 'Jack:', 'Sarah:825', 'Jack:45', 'Brandon:10', 'James:3.25', 'James:125.62', 'Sarah:2.43', 'Brandon:100.5']
  1. Define two lists, names and numbers to later store the retrieved names and numbers.
names = []
numbers = []
  1. Iterate through the lst list, and split each string at the ":":
for s in lst:
    s.split(":")
  1. Now, assign the two values in the s.split(":") list into two variables, name and number:
name, number = s.split(":")
  1. Finally, append the strings into the two lists created eariler:
names.append(name)
numbers.append(number)

Altogether:

lst = ['Brandon:5', 'Patrick:18.9', 'Brandon:xyz', 'Jack:', 'Sarah:825', 'Jack:45', 'Brandon:10', 'James:3.25', 'James:125.62', 'Sarah:2.43', 'Brandon:100.5']
names = []
numbers = []
for s in lst:
    name, number = s.split(":")
    names.append(name)
    numbers.append(number)
print(names)
print(numbers)

Output:

['Brandon', 'Patrick', 'Brandon', 'Jack', 'Sarah', 'Jack', 'Brandon', 'James', 'James', 'Sarah', 'Brandon']
['5', '18.9', 'xyz', '', '825', '45', '10', '3.25', '125.62', '2.43', '100.5']
Red
  • 26,798
  • 7
  • 36
  • 58