0

I want to delete points(".") at the end of every word.

My code looks like this:

a = [('hello.',0) , ('foji.',0),('you',0)]
print([s.strip('.') for s in a])

The output should look something like: [('hello',0) , ('foji',0), ('you',0)]

I get an error says tuple object has no attribute strip! even if i use lists instead i get the same error !

note : using replace doesn't work too.

What should I do to fix this?

  • it doesnt work with replace too! – determindedElixir Jul 22 '20 at 16:25
  • 1
    Does this answer your question? [Remove specific characters from a string in Python](https://stackoverflow.com/questions/3939361/remove-specific-characters-from-a-string-in-python) – Nicolas Gervais Jul 22 '20 at 16:26
  • Think it through. When you iterate with `for s in a`, what do the `s` values look like? What kind of thing are they? Can you `.strip()` that? What is the thing you actually want to call `.strip()` on? Given the `s` value, how do you access that thing? Now, can you figure out the rule to create the desired elements? – Karl Knechtel Jul 22 '20 at 16:32
  • i already tried that. it doesnt work – determindedElixir Jul 22 '20 at 16:32
  • 1
    To close voters: this is not a duplicate. The problem is not with the logic for manipulating the string; the problem is **accessing** the string. – Karl Knechtel Jul 22 '20 at 16:33
  • @KarlKnechtel indexing a tuple is as duplicate as it gets – Nicolas Gervais Jul 22 '20 at 17:46
  • It's not a duplicate *of what was marked*. And the issue is that OP needs to combine a few concepts - indexing the tuple, applying the transformation to the appropriate part of the tuple, creating the new tuple from there, and wrapping it all up in a comprehension - as in the accepted answer. It's maybe not the best "reference" question for future search-engine users, but it's certainly a reasonably original question. – Karl Knechtel Jul 22 '20 at 21:24
  • how is a list of tuples? – determindedElixir Jul 23 '20 at 08:17

2 Answers2

3
  a = [('hello.',0) , ('foji.',0),('you',0)]
  print([(s[0].replace('.', ''), s[1]) for s in a])
 

Output:

  [('hello', 0), ('foji', 0), ('you', 0)]
3

You are working with tuples inside the list so every element is (element1,element2) change your print to

print([(s[0].strip('.'),s[1]) for s in a])
Julio González
  • 71
  • 1
  • 1
  • 5