-2

For example: list1 = [1t, 1r, 2t, 2r, 3t, 3r...., nt, nr]. How do I make a list list_t that has all t items from list1? I tried using the following for loop:

for i in list1[0:]:
    list_t =[i.t]

But this only assigns the first item to list_t.

1 Answers1

0

If your list has the same items repeated at the same time step, then:

list1 = ['1t', '1r', '2t', '2r', '3t', '3r']

# list[start:stop:step]
l2 = list1[0::2]
print(l2)

will solve your problem.

However, if what you mean is that you have a list of strings and you need to extract the strings with t on it, then you can just test if t is in the element, like so:

l2 = list()
for i in list1:
    if 't' in i :
        l2.append(i)

print(l2)
B Furtado
  • 1,488
  • 3
  • 20
  • 34