Python Forum
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
remove apostrophes in list
#1
Is there a way to remove apostrophes in list so that this:

a_list = ['mary', 'had', 'a', 'little', 'lamb']
becomes:

a_list = [mary, had, a, little, lamb]
Thank you
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#2
I'm sure that you have tried a number of options.

This may not give you the complete solution but what about, rebuilding the list along this idea:

 b_list = a_list[0]+"  "+a_list[1]+" "+a_list[2]
etc.

Giving you a result of:
b_list =  mary had a little lamb
Obviously you can add back any commas, but as my experience is python limited, I not sure of how to ensure that the b_list becomes a true list with [] etc.

Can you give us an idea of your reasons as this may help with a solution.

"The good thing about standards is that you have so many to choose from" Andy S. Tanenbaum
Reply
#3
But then they wouldn't be strings anymore, and it'd suddenly become invalid syntax. What are you REALLY trying to do? :p
Reply
#4
Fair enough. In a previous post (else-statement-not-executing) it was intimated that a preferred method within menus was the use of dictionaries. Which is fine, if the dictionary is hard coded, however, in my particular case, neither the 'key' nor the value are known in advance. As you say, every attempt I try always puts the 'value' back as a string.

It's not a deal breaker, as the 'eval' option mentioned in the previous post solves the problem, just wanted to know if it was possible.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#5
If the object you're trying to refeclarence is contained within a class/object/module, you could use getattr() to find it:
>>> class Spam:
...   def foo(self):
...     return "bar"
...
>>> dispatcher = Spam()
>>> method = getattr(dispatcher, "foo")
>>> method()
'bar'
If it's not contained within anything (...and it probably should be), then you can do something similar with locals():
>>> def foo():
...   return 42
...
>>> locals()["foo"]()
42
>>>
I'd say either option is better than using eval(), as then you can at least use hasattr() to check if something exists and is a callable before blindly trying to run it.
Reply
#6
Duly noted, thank you. I'll give it a try.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to remove all elements from list based on a condition sg_python 3 441 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  Remove numbers from a list menator01 4 1,328 Nov-13-2022, 01:27 AM
Last Post: menator01
  Remove empty keys in a python list python_student 7 3,027 Jan-12-2022, 10:23 PM
Last Post: python_student
  Remove an item from a list contained in another item in python CompleteNewb 19 5,724 Nov-11-2021, 06:43 AM
Last Post: Gribouillis
  .remove() from a list - request for explanation InputOutput007 3 2,228 Jan-28-2021, 04:21 PM
Last Post: InputOutput007
  Remove double quotes from the list ? PythonDev 22 8,793 Nov-05-2020, 04:53 PM
Last Post: snippsat
  Remove specific elements from list with a pattern Xalagy 3 2,701 Oct-11-2020, 07:18 AM
Last Post: Xalagy
  How to remove dict from a list? Denial 7 2,941 Sep-28-2020, 02:40 PM
Last Post: perfringo
  Remove all \n from end of list items JackMack118 4 36,258 Dec-27-2019, 08:34 AM
Last Post: perfringo
  can't remove element from a list yokaso 6 2,917 Nov-06-2019, 03:11 PM
Last Post: yokaso

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020