-1

I have a list like so:

[("How", 0.23),("Like", 1.23),("Book",0.53)]

All I want is to loop through the list and put the strings into a new list like this:

["How", "like", "Book"]

I know a nested loop can do it, but is there any simpler way to do it?

Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
Collander
  • 41
  • 9

1 Answers1

1

Use list comprehension. Following that, either access the string as a first element of each tuple by indexing, or unpack the tuple into 2 elements:

my_list = [("How", 0.23),("Like", 1.23),("Book",0.53)]

# Get the first element by indexing:
my_strings = [x[0] for x in my_list]

# OR:

# Unpack the tuple:
my_strings = [s for s, n in my_list]
Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47