KBOB
KBOB

Reputation: 13

Python taking a certain string from a list

I have a list of informations like this in a list:

example = [
    ['68', ' ?', ' 38317', ' 1st-4th', ' 2', ' Divorced', ' ?', ' Not-in-family', ' White', ' Female', ' 0', ' 0', ' 20', ' United-States', ' <=50K'],
    ['32', ' ?', ' 293936', ' 7th-8th', ' 4', ' Married-spouse-absent', ' ?', ' Not-in-family', ' White', ' Male', ' 0', ' 0', ' 40', ' ?', ' <=50K']
]

aka example[0] would be:

['68', ' ?', ' 38317', ' 1st-4th', ' 2', ' Divorced', ' ?', ' Not-in-family', ' White', ' Female', ' 0', ' 0', ' 20', ' United-States', ' <=50K']

and example[1] would be:

['32', ' ?', ' 293936', ' 7th-8th', ' 4', ' Married-spouse-absent', ' ?', ' Not-in-family', ' White', ' Male', ' 0', ' 0', ' 40', ' ?', ' <=50K']

I want to only take the last bit of it, ' <=50K', how do I do that?

Upvotes: 1

Views: 75

Answers (5)

Anton Zuenko
Anton Zuenko

Reputation: 751

do this:

from operator import itemgetter
last = map(itemgetter(-1), example)
print(last)

Upvotes: 0

New2WebDevelopment
New2WebDevelopment

Reputation: 146

By default you can index items from a list from the left side of the list to right. Indexing means to get items from a list based on their numeric position in the list. For example:

myList = [1,2,3,4,5,6]
myList[0] # This would give me the 1st item of the list; the number 1
myList[1] # would give me the number 2 and so on....

If you want to select items from end of your list there are a number of ways to do this, but the easiest way would look something like this:

myList[-1] # this would get me the last item of the list; 6 
myList[-2] # would get the second to last item
myList[-3] # would get the third to last item
myList[-4] # would get the fourth to last item

Think about this like reverse indexing. So instead of getting items from left to right, you get items from the right to left.

Use positive numbers when you want to get items starting from the left of the list and negative numbers when you want to get items starting from the right.

So your code:

['68', ' ?', ' 38317', ' 1st-4th', ' 2', ' Divorced', ' ?', ' Not-in-family', ' White', ' Female', ' 0', ' 0', ' 20', ' United-States', ' <=50K']
['32', ' ?', ' 293936', ' 7th-8th', ' 4', ' Married-spouse-absent', ' ?', ' Not-in-family', ' White', ' Male', ' 0', ' 0', ' 40', ' ?', ' <=50K']

Would look something like this:

lastItem = list_of_information[0][-1]

Upvotes: 1

Martijn Pieters
Martijn Pieters

Reputation: 1121266

You can get the last element of all nested lists with a list comprehension:

[sublist[-1] for sublist in example]

This would produce [' <=50k', ' <=50k'].

Demo:

>>> example = [
...     ['68', ' ?', ' 38317', ' 1st-4th', ' 2', ' Divorced', ' ?', ' Not-in-family', ' White', ' Female', ' 0', ' 0', ' 20', ' United-States', ' <=50K'],
...     ['32', ' ?', ' 293936', ' 7th-8th', ' 4', ' Married-spouse-absent', ' ?', ' Not-in-family', ' White', ' Male', ' 0', ' 0', ' 40', ' ?', ' <=50K'],
... ]
>>> [sublist[-1] for sublist in example]
[' <=50K', ' <=50K']

You could also just address each nested list directly and extract the last element:

>>> example[0][-1]
' <=50K'
>>> example[1][-1]
' <=50K'

Upvotes: 2

thumbsupep
thumbsupep

Reputation: 181

To get the last element of a list within a list, you would do

list[0][len(list[0]) -1]

Upvotes: -1

brian
brian

Reputation: 131

You could loop if you're looking for a not-particularly-fast way of coding it:

new_list = []
for smaller_list in big_list:
     new_list.append(smaller_list[-1])

Upvotes: 0

Related Questions