Trcx
Trcx

Reputation: 4454

Get the first character of the first string in a list?

How would I get the first character from the first string in a list in Python?

It seems that I could use mylist[0][1:] but that does not give me the first character.

>>> mylist = []
>>> mylist.append("asdf")
>>> mylist.append("jkl;")
>>> mylist[0][1:]
'sdf'

Upvotes: 121

Views: 726286

Answers (5)

cottontail
cottontail

Reputation: 23141

If your list includes non-strings, e.g. mylist = [0, [1, 's'], 'string'], then the answers on here would not necessarily work. In that case, using next() to find the first string by checking for them via isinstance() would do the trick.

next(e for e in mylist if isinstance(e, str))[:1]

Note that ''[:1] returns '' while ''[0] spits IndexError, so depending on the use case, either could be useful.

The above results in StopIteration if there are no strings in mylist. In that case, one possible implementation is to set the default value to None and take the first character only if a string was found.

first = next((e for e in mylist if isinstance(e, str)), None)
first_char = first[0] if first else None

Upvotes: 1

Eric Leschinski
Eric Leschinski

Reputation: 153882

Get the first character of a bare python string:

>>> mystring = "hello"
>>> print(mystring[0])
h
>>> print(mystring[:1])
h
>>> print(mystring[3])
l
>>> print(mystring[-1])
o
>>> print(mystring[2:3])
l
>>> print(mystring[2:4])
ll

Get the first character from a string in the first position of a python list:

>>> myarray = []
>>> myarray.append("blah")
>>> myarray[0][:1]
'b'
>>> myarray[0][-1]
'h'
>>> myarray[0][1:3]
'la'

Numpy operations are very different than python list operations.

Python has list slicing, indexing and subsetting. Numpy has masking, slicing, subsetting, indexing.

These two videos cleared things up for me.

"Losing your Loops, Fast Numerical Computing with NumPy" by PyCon 2015: https://youtu.be/EEUXKG97YRw?t=22m22s

"NumPy Beginner | SciPy 2016 Tutorial" by Alexandre Chabot LeClerc: https://youtu.be/gtejJ3RCddE?t=1h24m54s

Upvotes: 43

agf
agf

Reputation: 176780

You almost had it right. The simplest way is

mylist[0][0]   # get the first character from the first item in the list

but

mylist[0][:1]  # get up to the first character in the first item in the list

would also work.

You want to end after the first character (character zero), not start after the first character (character zero), which is what the code in your question means.

Upvotes: 173

Artsiom Rudzenka
Artsiom Rudzenka

Reputation: 29103

Indexing in python starting from 0. You wrote [1:] this would not return you a first char in any case - this will return you a rest(except first char) of string.

If you have the following structure:

mylist = ['base', 'sample', 'test']

And want to get fist char for the first one string(item):

myList[0][0]
>>> b

If all first chars:

[x[0] for x in myList]
>>> ['b', 's', 't']    

If you have a text:

text = 'base sample test'
text.split()[0][0]
>>> b

Upvotes: 22

Constantinius
Constantinius

Reputation: 35059

Try mylist[0][0]. This should return the first character.

Upvotes: 4

Related Questions