Remove index spaces in a list

Asked

Viewed 57 times

0

In the list below, I want to print only the gender of each index, which, in fact, I can, but as you can notice there are 4 lists that are without definition of gender, which causes, when printing several spaces. I would like to print the genres in a more organized way, formatting the text. See how was my output below in the example.

['2017-01-01 00:02:54', '2017-01-01 00:08:21', '327', 'Larrabee St & Menomonee St', 'Sheffield Ave & Kingsbury St', 'Subscriber', 'Male', '1984.0']
['2017-01-01 00:06:06', '2017-01-01 00:18:31', '745', 'Orleans St & Chestnut St (NEXT Apts)', 'Ashland Ave & Blackhawk St', 'Subscriber', 'Male', '1985.0']
['2017-01-01 00:07:28', '2017-01-01 00:12:51', '323', 'Franklin St & Monroe St', 'Clinton St & Tilden St', 'Subscriber', 'Male', '1990.0']
['2017-01-01 00:07:57', '2017-01-01 00:20:53', '776', 'Broadway & Barry Ave', 'Sedgwick St & North Ave', 'Subscriber', 'Male', '1990.0']
['2017-01-01 00:10:44', '2017-01-01 00:21:27', '643', 'State St & Kinzie St', 'Wells St & Polk St', 'Subscriber', 'Male', '1970.0']
['2017-01-01 00:11:34', '2017-01-01 00:23:47', '733', 'Wabash Ave & Wacker Pl', 'Clinton St & Tilden St', 'Subscriber', 'Male', '1986.0']
['2017-01-01 00:14:57', '2017-01-01 00:26:22', '685', 'Daley Center Plaza', 'Canal St & Monroe St (*)', 'Customer', '', '']
['2017-01-01 00:15:03', '2017-01-01 00:26:28', '685', 'Daley Center Plaza', 'Canal St & Monroe St (*)', 'Customer', '', '']
['2017-01-01 00:17:01', '2017-01-01 00:29:49', '768', 'Dayton St & North Ave', 'Ogden Ave & Chicago Ave', 'Customer', '', '']
['2017-01-01 00:17:13', '2017-01-01 11:03:34', '38781', 'Wilton Ave & Diversey Pkwy', 'Halsted St & Wrightwood Ave', 'Subscriber', 'Female', '1988.0']
['2017-01-01 00:18:28', '2017-01-01 00:31:05', '757', 'Canal St & Madison St', 'LaSalle St & Illinois St', 'Customer', '', '']
['2017-01-01 00:18:50', '2017-01-01 00:21:47', '177', 'Theater on the Lake', 'Lakeview Ave & Fullerton Pkwy', 'Subscriber', 'Male', '1991.0']
['2017-01-01 00:23:41', '2017-01-01 00:29:13', '332', 'Halsted St & Maxwell St', 'Halsted St & 18th St', 'Subscriber', 'Male', '1984.0']
['2017-01-01 00:25:47', '2017-01-01 00:39:53', '846', 'Ravenswood Ave & Lawrence Ave', 'Clarendon Ave & Gordon Ter', 'Subscriber', 'Female', '1987.0']
['2017-01-01 00:25:47', '2017-01-01 00:43:23', '1056', 'Clark St & Congress Pkwy', 'Wolcott Ave & Polk St', 'Subscriber', 'Male', '1984.0']
['2017-01-01 00:26:21', '2017-01-01 00:39:40', '799', 'Ravenswood Ave & Lawrence Ave', 'Clarendon Ave & Gordon Ter', 'Subscriber', 'Male', '1987.0']
['2017-01-01 00:27:21', '2017-01-01 00:42:59', '938', 'Millennium Park', 'Michigan Ave & 18th St', 'Subscriber', 'Male', '1991.0']
['2017-01-01 00:27:28', '2017-01-01 00:42:44', '916', 'Millennium Park', 'Michigan Ave & 18th St', 'Subscriber', 'Female', '1990.0']
['2017-01-01 00:27:45', '2017-01-01 00:31:13', '208', 'Damen Ave & Chicago Ave', 'Damen Ave & Division St', 'Subscriber', 'Male', '1982.0']
['2017-01-01 00:27:52', '2017-01-01 00:33:46', '354', 'Paulina Ave & North Ave', 'Damen Ave & Division St', 'Subscriber', 'Female', '1982.0']

My way out was like this:

Male
Male
Male
Male
Male
Male



Female

Male
Male
Female
Male
Male
Male
Female
Male
Female
  • "more organized form"? What is it? Can you give an example of what you want pff? What code do you have?

  • without the spaces , @Miguel , note that I printed the genres , but as it is a project I wanted to leave the text more formatted , understand?

  • for i in range(Len(data_list[2:22])): print(data_list[i],[6]) I used this code, loop for to access the rows and indexes .

  • 1

    This is it: https://repl.it/repls/ThistleSereneGeeklog

  • thanks , I will test , but what would this exclamation ? I had already seen it but forgot the meaning .

  • What question? I don’t understand

  • 1

    I think it would be exclamation, you’re making the denial I think.

  • yes yes, exclamation. understood now.

  • 1

    != means 'different from', the opposite of ==, 'equal to'.

  • 1

    @Miguel, thanks again. I’ll test the code.

Show 5 more comments

1 answer

3


You can define a generator that will take the non-null genres from your data, then iterate it, printing the result. For example:

genres = (columns[6] for columns in data if columns[6])

for genre in genres:
    print(genre)

See working on Repl.it | Ideone | Github GIST

If these lists represent a related set of data, which it seems to be, I strongly recommend that you research on NamedTuple.

When to use lists and when to use tuples?

  • 1

    Nice, python never ceases to amaze me!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.