Chalarangelo / 30-seconds-of-python

Short Python code snippets for all your development needs
https://www.30secondsofcode.org/python/p/1
Creative Commons Attribution 4.0 International
8.83k stars 1.26k forks source link

List of possible snippets #369

Closed gurukiran07 closed 4 years ago

gurukiran07 commented 4 years ago

I put together a list of questions which I believe are useful. Some of them may have been already covered in the snippets.

Python questions frequently asked on SO:

1. Round to a base value

Example:

# round to a base value 5
10 -> 10
12 -> 10
13 -> 15
14 -> 15
16 -> 15
18 -> 20

2. Check if a string contains a given sub-string

Example:

our_string = "30secondsofpython"
sub_string = "python"
answer -> True

3. Sorted a dictionary by key or value

Example:

# sorted by value
my_dict = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
output -> {0: 0, 2: 1, 1: 2, 4: 3, 3: 4}

4. Selecting a random element from a given sequence

Example:

seq = ['a', 'b', 'c', 'd']
output -> 'c' # selected randomly each time.

5. Join a sequence with a given delimiter

Example:

seq = ['a', 'b', 'c', 'd']
delim = '-'
output -> "a-b-c-d"

6. Determine the type of a variable

Example:

a = 10
a_type -> int
b = "string"
b_type -> str

7. Pad a string with given value to make it a specific length

Example:

our_str = "a"
pad_val = "0"
length = 5
output -> "0000a"

8. Convert two lists into a dictionary

Example:

a = ['name', 'place', 'animal', 'thing']
b = ['dexter', 'denver', 'deer', 'door']
output -> {'name':'dexter', 'place':'denver', 'animal':'deer', 'thing':'door'}

9. Trim whitespace from a string.

Example:

our_string = "   30secs of python  "
output -> "30secs of python"

10. Determine if an object is iterable

Example:

a = 10
output -> False
b = [1, 2, 3]
output -> True
c,d,e,f,g = {1:2}, {1,2}, frozenset([1,2,3]), "string", (1,2,3)
output -> True, True, True, True, True

11. Return multiple values

This question has two answers, 1 return a list of all possible outputs. 2 Using yield, a generator function. IMO, we don't have any snippets related to yield. We can introduce one of the Python's core functionality using yield


12. Check if a value is NaN

Example:

a = float('nan')
output -> True

13. Return ASCII value of a character and vice-versa.

Example:

a = 'A'
output -> 64
b = 64
output -> 'A'

14. Remove duplicate from a list whilst preserving the order

Using set doesn't gaurentee the order of the list. Example:

a = [1,1,2,3,1]
output -> [1, 2, 3]
# Simple example to prove set doesn't preserve order
l = [1,1,8]
set(l)
# {8, 1}
# As 8 and 1 have same hash values

15. Efficient way to remove unordered duplicates

Example:

a = [(1,2), (2,1), (1,2,3), (2,1,3), (3,2,1)]
output -> [(1,2), (1,2,3)]
# There's no hard and fast rule that we keep fisrt ocurrence, any one of them is fine, 
# i.e (1,2,3), (2,1,3), (3,2,1) are duplicates keep any one of them.

This list is not exhaustive as of now. I can add more to the list over time. I don't if questions related to files, directories, os, datetime are relevant to this repo. If yes, I can add them too.

Navaneethnanda commented 4 years ago

I'm adding one more question which has a daily use. 1. Sum of elements in lists of arbitrary number.

input=[1,2,3,4,5],[4,2,6,2,3],[1,9,1,5,2]
#any number of lists can be passed as arguments SumOfLists([1,2,3,4,5],[4,2,6,2,3],[1,9,1,5,2])
output=[6,13,10,11,10]
96RadhikaJadhav commented 4 years ago

Hi @gurukiran07 I would like to work on this issue as a part of hacktoberfest. Could you please assign this to me?

coralhayoun commented 4 years ago

Hi, @gurukiran07 I would be glad to help you with this pr as part of the hacktoberfest month Can you assigned me in? 😄

Chalarangelo commented 4 years ago

Thanks for taking the time to compile this list. However, some of these are not worth adding (as they are either built-ins or very simple) and others have already been added in the meantime.