Convert certain elements to int type in list of tuple

I am trying to convert elements at index[2] to int type using this func that I wrote. The data in the csv file looks like this

`
id neighbourhood_group room_type price
2539 Brooklyn Private room 149
2595 Manhattan Entire home/apt 225
3647 Manhattan Private room 150

`

def load_dt():
    with open(filename) as f:
# skip the header
        next(f, None)
# line[1:] skip first column
        data = [tuple(line[1:]) for line in csv.reader(f)]
 
     return data

The output of the code:

[('Manhattan', 'Shared room', '55'), ('Manhattan', 'Private room', '90')]

What I need is that index[2] = type int and maintaining the same data structure because my previous work built on the same data structure list of tupes

If line is a list (since you’re slicing it), you could rewrite it as

        data = [tuple([line[1], line[2], line[3]]) for line in csv.reader(f)]

This is exactly how I wanted, thanks a lot. I just added int to line[3] and now I got this:

Now I can do some operations.
[('Manhattan', 'Shared room', 55), ('Manhattan', 'Private room', 90)]

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.