-2

I have the below list of lists

[['Afghanistan,2.66171813,7.460143566,0.490880072,52.33952713,0.427010864,-0.106340349,0.261178523'], ['Albania,4.639548302,9.373718262,0.637698293,69.05165863,0.74961102,-0.035140377,0.457737535']]

I want to create a new list with only the country names.

So

[Afghanistan, Albania]

Currently using this code.

with open(fileName, "r") as f:
    _= next(f)
    row_lst = f.read().split()
    countryLst = [[i] for i in row_lst]

2 Answers2

1

Try this, using split(',') as your first element in list of list is string separated by comma.

>>> lst = [['Afghanistan,2.66171813,7.460143566,0.490880072,52.33952713,0.427010864,-0.106340349,0.261178523'], ['Albania,4.639548302,9.373718262,0.637698293,69.05165863,0.74961102,-0.035140377,0.457737535']]

Output:

>>> [el[0].split(',')[0] for el in lst]                       
['Afghanistan', 'Albania']

Explanation:

# el[0] gives the first element in you list which a string.
# .split(',') returns a list of elements after spliting by `,`
# [0] finally selecting your first element as required.

Edit-1:

Using regex,

pattern = r'([a-zA-Z]+)'
new_lst = []
for el in lst:
    new_lst+=re.findall(pattern, el[0])

>>> new_lst     # output          
['Afghanistan', 'Albania']
shaik moeed
  • 5,300
  • 1
  • 18
  • 54
  • This worked but when putting split(',') first in the row_lst it gives me an error. Putting the code you shared above under countryLst gave me what I want but I don't think its efficient – Electric_Sheep01 Sep 19 '19 at 04:42
  • @Electric_Sheep01 *but I don't think its efficient* why? You are not doing any extra steps to achieve results. – shaik moeed Sep 19 '19 at 04:46
0

Looks like a CSV file. Use the csv module

Ex:

import csv

with open(fileName, "r") as f:
   reader = csv.reader(f)
   next(reader) #Skip header
   country = [row[0] for row in reader]
Rakesh
  • 81,458
  • 17
  • 76
  • 113