Skip to content

Commit 3e7a5a5

Browse files
committed
join() tips addition
1 parent 5cecb89 commit 3e7a5a5

File tree

2 files changed

+65
-4
lines changed

2 files changed

+65
-4
lines changed

Diff for: Tips & Tricks/Lambda Function.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,31 @@ Here's its features and a quick shortcuts to get you up to speed with lambda fun
1919
return x + y
2020
```
2121

22-
3. **Single Expression Only**: Lambda functions can only contain a single expression.
22+
2. **Single Expression Only**: Lambda functions can only contain a single expression.
2323

2424
Example:
2525
```python
2626
# This lambda function adds 10 to the input number
2727
add_ten = lambda x: x + 10
2828
```
2929

30-
4. **No Statements**: Lambda functions cannot contain statements, only expressions.
30+
3. **No Statements**: Lambda functions cannot contain statements, only expressions.
3131

3232
Example:
3333
```python
3434
# Incorrect: Lambda functions cannot contain statements like print, because it's assigning obtained value to vaiable.
3535
greet = lambda name: print(f"Hello, {name}!")
3636
```
3737

38-
5. **Anonymous Functions**: Lambda functions are anonymous, meaning they don't have a name.
38+
4. **Anonymous Functions**: Lambda functions are anonymous, meaning they don't have a name.
3939

4040
Example:
4141
```python
4242
# Anonymous lambda function to check if a number is even
4343
is_even = lambda x: x % 2 == 0
4444
```
4545

46-
6. **Use Cases**: Lambda functions are handy for short, one-off functions, especially when used with functions like `map()`, `filter()`, and `sorted()`.
46+
5. **Use Cases**: Lambda functions are handy for short, one-off functions, especially when used with functions like `map()`, `filter()`, and `sorted()`.
4747

4848
Example:
4949
```python

Diff for: Tips & Tricks/join() usage.md

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
🔓**Python `.join()` Power and Secret Tips/Tricks**:
2+
3+
1. **String Concatenation with `.join()`**: Instead of using `+` for string concatenation, use `.join()` for better performance, especially with large strings.
4+
```python
5+
words = ['Hello', 'world', '!']
6+
sentence = ' '.join(words)
7+
```
8+
9+
2. **List to String Conversion**: Use `.join()` to convert a list of strings into a single string efficiently.
10+
```python
11+
words = ['Python', 'is', 'awesome']
12+
sentence = ' '.join(words)
13+
```
14+
15+
3. **Joining Iterable of Integers**: Convert a list of integers to a single string efficiently.
16+
```python
17+
numbers = [1, 2, 3, 4, 5]
18+
num_string = ''.join(map(str, numbers))
19+
```
20+
21+
4. **Joining Generator Expression**: Utilize a generator expression within `.join()` for memory efficiency.
22+
```python
23+
data = ['a' * i for i in range(100000)]
24+
result = ''.join(item for item in data)
25+
```
26+
27+
5. **Custom Delimiter**: Specify a custom delimiter other than space for joining elements.
28+
```python
29+
words = ['apple', 'banana', 'orange']
30+
csv_string = ','.join(words)
31+
```
32+
33+
6. **Joining Nested Lists**: Flatten a nested list efficiently using `.join()` with list comprehension.
34+
```python
35+
nested_list = [['a', 'b', 'c'], ['1', '2', '3'], ['x', 'y', 'z']]
36+
flat_list = ''.join(item for sublist in nested_list for item in sublist)
37+
```
38+
39+
7. **Joining Lines from File**: Concatenate lines from a file efficiently using `.join()` and file reading.
40+
```python
41+
with open('file.txt', 'r') as file:
42+
file_content = ''.join(file.readlines())
43+
```
44+
45+
8. **Joining Elements with Formatting**: Apply formatting while joining elements for custom output.
46+
```python
47+
data = [('apple', 3), ('banana', 5), ('orange', 2)]
48+
output = '\n'.join(f'{item[0]}: {item[1]}' for item in data)
49+
```
50+
51+
9. **Joining Set of Strings**: Join a set of strings while preserving uniqueness and order.
52+
```python
53+
unique_words = {'apple', 'banana', 'orange'}
54+
sentence = ' '.join(unique_words)
55+
```
56+
57+
10. **Joining with Newline Character**: Concatenate elements with newline character for multiline output.
58+
```python
59+
lines = ['First line', 'Second line', 'Third line']
60+
multiline_string = '\n'.join(lines)
61+
```

0 commit comments

Comments
 (0)