DEV Community

Many Things You Can Do With Python Lists You May Have Missed

John Au-Yeung on February 05, 2020

Subscribe to my email list now at http://jauyeung.net/subscribe/ Follow me on Twitter at https://twitter.com/AuMayeung Many more articles at http...
Collapse
 
waylonwalker profile image
Waylon Walker

Great post, I had no idea there was a clear() method. You're missing out on one of my favorite features though! The * operator. I commonly use it along side list comprehensions.

For example take a pandas DataFrame and get all of the columns that include "price" in their name.

df_prices = df[col for col in df.columns if 'price' in col]
Collapse
 
aumayeung profile image
John Au-Yeung

Thanks for reading.

You mean make a filtered list with lost comprehension?

Or you mean using * to repeat items?

Collapse
 
waylonwalker profile image
Waylon Walker • Edited

Lol, good catch. 😳 I think I took a break between the comment and finding a good example. Here is a real example of nearly the same thing if the DataFrame was a pyspark DataFrame. pyspark.select takes in column names as *args df.select('us_price', 'eu_price')

They can be selected programatically with a list comprehension and *unpacking.

df_prices = df.select(*[col for col in df.columns if 'price' in col])
Thread Thread
 
aumayeung profile image
John Au-Yeung

I see. The list comprehension is used as a predicate?

Thread Thread
 
waylonwalker profile image
Waylon Walker • Edited

Not sure what you mean by predicate, the list comprehension is a filter.

price_cols = [col for col in df.columns if 'price' in col]
df_prices = df.select(*price_cols)

Here is a generally relatable example using the print statement

[ins] In [1]: fruits = ['apple', 'orange', 'orange', 'grape', 'pear']

[ins] In [2]: print(fruits)
['apple', 'orange', 'orange', 'grape', 'pear']

[ins] In [3]: print('apple', 'orange', 'orange', 'grape', 'pear')
apple orange orange grape pear

[ins] In [4]: print(*fruits)
apple orange orange grape pear

[ins] In [5]: print(*[fruit for fruit in fruits if 'p' in fruit])
apple grape pear
Thread Thread
 
aumayeung profile image
John Au-Yeung

That makes sense. I haven't seen this documented in many places. That's probably why I missed this.

Collapse
 
maximus06 profile image
Maximus06

Thanks John for this nice summary on the list. I think you forget a banana in the print of fruit_copy after fruit_copy.append('banana') and I like the bananas ;)

Collapse
 
aumayeung profile image
John Au-Yeung

Thanks so much for reading and catching that mistake. I corrected it now.

Collapse
 
chachan profile image
Cherny

what's the difference between l.count() and len(l)?

Collapse
 
aumayeung profile image
John Au-Yeung

There's no difference.

Collapse
 
kedark profile image
Kedar Kodgire

I guess there is a huge difference between them

  • count returns the number of occurrences of a particular element in the list
  • length returns the length of the list

check the example below

>>> vowels = ['a', 'e', 'i', 'o', 'i', 'u']
>>> vowels.count('a')
1
>>> len(vowels)
6
Thread Thread
 
aumayeung profile image
John Au-Yeung

Yes. You're right