Python Daily

I post Python tips here on an almost daily basis both for myself and everyone else.

Hope you find this useful.

Python Trick #1:

Write a function to check if a list has duplicates in it.

For example,

If the function receives [ 1, 2, 1, 3], it should return true.

To solve this, we can use the property that Python sets contain only unique elements i.e., no duplicates.

We convert the list into a set. Then, we check if both the set and list are of the same length.

They will be of different lengths if the list has duplicates.

Chaitanya Baweja
·
1 min
·
9 cards

Read “Python Daily” on a larger screen, or in the Medium app!