Python Forum
get a string from a list if it is there
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
get a string from a list if it is there
#1
i have a list that might have a string as the first item or the list might be empty that i am giving to an expression. if there is such a string, i want the expression to be that string. but if the list is empty, i want an empty string (e.g. ''). the list can only have a string as item #0 or be empty. i'm drawing a blank trying to figure this out. it is not a variable. it can only appear once since the list itself is the result of an expression. any ideas?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Something akin to this:

x = []
def do_stuff(x):
    try:
        if isinstance(x[0], str):
            return x[0]
        return ""
    except IndexError:
        return ""
Reply
#3
yeah, that's pretty easy and sure. but i need it as an expression. the list will have a string in item #0 if it is not empty, so you don't need to test what type is there. if something is there, it can only be a string. if the list is empty, the value of the expression needs to be '' (empty string).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
Why not use an if expression then? For example

>>> l = ["foo"]
>>> l[0] if l else ""
'foo'
Having said that, the fact you're focusing on needing an expression makes me worry that you're solving the wrong problem. Perhaps you could give more details about why you're doing things this way?
Reply
#5
se filter to get at the elements that have abc.

>>> lst = ['abc-123', 'def-456', 'ghi-789', 'abc-456']
>>> print filter(lambda x: 'abc' in x, lst)
['abc-123', 'abc-456']
Reply
#6
Need to check for empty list
def iscell0str(alist):
    if alist:
        return alist[0] if isinstance(alist[0], str) else ''
    return ''

# test
print(f"empty string: {iscell0str([])}")
print(f"cell 0 is string: {iscell0str(['has cell 0 str'])}")
print(f"cell 0 not a string: {iscell0str([4, 'Not cell 0'])}")
results:
Output:
empty string: cell 0 is string: has cell 0 str cell 0 not a string:
Reply
#7
maybe i do need to back up and solve a bigger issue. but this is the thing i'm working on, for now. the case in line 9 will never be a case to be considered. the list can only ever have strings be put in it. looking at it more broadly, i need to get whatever is in position #0 or get a ''. the code using this expects a string. but that expectation is fine. it just faces a possible empty list. i need to go look at that code, again.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I converted string to 'list', but it doesn't look like a list! mrapple2020 3 3,202 Apr-07-2019, 02:34 PM
Last Post: mrapple2020
  Create Alert if string from list appears on other list javalava 1 2,479 Sep-17-2018, 02:44 PM
Last Post: DeaD_EyE
  List of pathlib.Paths Not Ordered As Same List of Same String Filenames QbLearningPython 20 15,180 Nov-16-2017, 04:47 PM
Last Post: QbLearningPython
  Create a new list by comparing values in a list and string DBS 2 3,485 Jan-14-2017, 07:59 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

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