0

For example the list will contain

list1 = ["mathematics", "sciences", "historical"]

and i only need the middle parts of the string

output_list = ["thema", "ience", "stori"]
knl
  • 969
  • 11
  • 35
  • Does this answer your question? [Understanding slice notation](https://stackoverflow.com/questions/509211/understanding-slice-notation) – sushanth Apr 15 '21 at 03:56

2 Answers2

3

Python list comprehension. Please read it up, it's useful.

Also, python slicing and indexing. You can read more here.

For the problem you post:

output_list = [word[2:7] for word in list1]

print(output_list) 

knl
  • 969
  • 11
  • 35
0

You can use string slicing for this:

x = "abcde"
print(x[1:4])  # prints elements [1, 4), i.e., "bcd"
mackorone
  • 1,056
  • 6
  • 15