Last active
August 12, 2025 05:03
-
Star
(126)
You must be signed in to star a gist -
Fork
(12)
You must be signed in to fork a gist
-
-
Save Julynx/dd500d8ae7e335c3c84684ede2293e1f to your computer and use it in GitHub Desktop.
Revisions
-
Julynx revised this gist
Aug 10, 2023 . 1 changed file with 5 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -166,12 +166,12 @@ print(unique_cities) ```python cities = ['London', 'Paris', 'London'] unique_cities = list(set(cities)) print(unique_cities) >> ['London', 'Paris'] ``` -
Julynx revised this gist
Aug 10, 2023 . 1 changed file with 9 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -110,7 +110,7 @@ for name in names: text = text[:-2] print(text) >> 'Olivia, Nicholas, Violet' ``` </td> @@ -126,7 +126,7 @@ text = ', '.join(names) print(text) >> 'Olivia, Nicholas, Violet' ``` </td> @@ -158,7 +158,7 @@ for city in cities: unique_cities.append(city) print(unique_cities) >> ['London', 'Paris'] ``` </td> @@ -169,11 +169,11 @@ cities = ['London', 'Paris', 'London'] unique_cities = set(cities) print(unique_cities) >> {'London', 'Paris'} unique_cities = list(unique_cities) print(unique_cities) >> ['London', 'Paris'] ``` </td> @@ -386,7 +386,7 @@ text = ' '*((length//2) - (len(text)//2)) \ + ' '*((length//2) - (len(text)//2)) print(text) >> ' Hello, World ' ``` </td> @@ -401,7 +401,7 @@ text = text.center(length) print(text) >> ' Hello, World ' ``` </td> @@ -614,7 +614,7 @@ for number in numbers: print(result) >> 4 ``` </td> @@ -631,7 +631,7 @@ result = next((number print(result) >> 4 ``` </td> -
Julynx revised this gist
Aug 10, 2023 . 1 changed file with 58 additions and 79 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -184,9 +184,11 @@ print(unique_cities) ## Intermediate Tips ### 1) Instead of ```if variable == a or variable == b``` use ```if variable in (a, b)``` Instead of repeating a variable in different conditions of an ```if``` statement, check for belonging against a tuple of allowed values. This is shorter, less prone to error, and makes it easier to add or remove allowed values in the future. You can also use ```if min <= variable <= max``` or ```if variable in range(min, max + 1)``` to check if a variable is within a range. <table> <tr> @@ -205,7 +207,7 @@ if variable == a or variable == b: <td> ```python if variable in (a, b): res = do_something(variable) ``` @@ -215,36 +217,47 @@ if variable in {a, b}: <br> ### 2) Learn how to unpack tuples You probably already know you can unpack tuples like this ```first = tuple[0]``` and ```second = tuple[1]```. But did you know you can also do ```first, second = tuple```? If the tuple has more than two elements, you can use ```first, second, *rest = tuple``` to unpack the first two elements and assign the rest to a list called ```rest```. <table> <tr> <th>Instead of doing this...</th> <th>try this:</th> </tr> <tr> <td> ```python runners = 'John Mike Greg Luke Bob' positions = runners.split(' ') first = positions[0] second = positions[1] rest = positions[2:] ``` </td> <td> ```python runners = 'John Mike Greg Luke Bob' first, second, *rest = runners.split(' ') print(first) >> 'John' print(second) >> 'Mike' print(rest) >> ['Greg', 'Luke', 'Bob'] ``` </td> @@ -253,9 +266,11 @@ a = 0 if b > 10 else 5 <br> ### 3) Split long and difficult to read statements into multiple lines You can encase a statement in parentheses ```()``` and split it into multiple lines. This is useful for long statements that are difficult to read. <table> <tr> @@ -266,20 +281,24 @@ Learn that you can use ```or``` to set a fallback value to assign in case anothe <td> ```python ... return Popen(...).stdout.read().decode()... ``` </td> <td> ```python ... return (Popen(cmd, shell=True, stdout=PIPE) .stdout .read() .decode() .strip()) ``` </td> @@ -359,30 +378,30 @@ Set ```length = os.get_terminal_size().columns``` to center text relative to you <td> ```python text = 'Hello, World' length = 20 text = ' '*((length//2) - (len(text)//2)) \ + text \ + ' '*((length//2) - (len(text)//2)) print(text) # >> ' Hello, World ' ``` </td> <td> ```python text = 'Hello, World' length = 20 text = text.center(length) print(text) # >> ' Hello, World ' ``` </td> @@ -393,7 +412,7 @@ print(text) ## Advanced Tips ### 1) Learn to transform for loops into comprehensions It's usually better to use comprehensions over for loops in Python when possible, as they are usually faster and shorter. However, it can be difficult to transform long and complex for loops into comprehensions. @@ -500,22 +519,22 @@ Iterating is expensive in Python and most common operations that require iterati ```python numbers = [1, 3, 4, 5, 7, 9, 2] total = 0 for number in numbers: total += number avg = total / len(numbers) ``` </td> <td> ```python numbers = [1, 3, 4, 5, 7, 9, 2] avg = sum(numbers) / len(numbers) @@ -618,43 +637,3 @@ print(result) </td> </tr> </table> -
Julynx revised this gist
May 11, 2023 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -634,8 +634,8 @@ Here are some secret extra tips, for making it all the way to the end. ### 2) Use ```all()``` and ```any()``` - ```all(iterator)``` returns ```True``` if all the items of the iterator are ```True```. - ```any(iterator)``` returns ```True``` if at least one of the items of the iterator is ```True```. You can use them as conditions in your ```if``` statements. -
Julynx revised this gist
May 11, 2023 . No changes.There are no files selected for viewing
-
Julynx revised this gist
May 11, 2023 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -655,4 +655,6 @@ Remember the [earlier example](#1-learn-to-transform-complex-for-loops-into-comp You can use ```map()``` to make it even more compact, by doing: ```new_items = map(process_item, items)``` ```map()``` will apply the ```process_item()``` function to each ```item``` in ```items``` and return the results as an iterator. You may want to transform it into a list by doing: ```list(new_items)``` -
Julynx revised this gist
May 11, 2023 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -655,4 +655,4 @@ Remember the [earlier example](#1-learn-to-transform-complex-for-loops-into-comp You can use ```map()``` to make it even more compact, by doing: ```new_items = map(process_item, items)``` ```map()``` will apply the ```process_item()``` function to each ```item``` in ```items``` and return the results as an iterator. You may want to transform it into a list by doing ```list(new_items)```. -
Julynx revised this gist
May 11, 2023 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -653,6 +653,6 @@ Keep in mind that ```min``` is included in the ```range()```, but ```max``` is n ### 4) Use ```map()``` to apply a function to multiple elements and collect the results Remember the [earlier example](#1-learn-to-transform-complex-for-loops-into-comprehensions) when we were doing ```new_items = [process_item(item) for item in items]```? You can use ```map()``` to make it even more compact, by doing: ```new_items = map(process_item, items)``` ```map()``` will apply the ```process_item()``` function to each ```item``` in ```items``` and return the results. -
Julynx revised this gist
May 11, 2023 . 1 changed file with 10 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -646,4 +646,13 @@ Instead of writing separate conditions (```if min < value and value < max:```): - Use intervals: ```if min < value < max:``` - Or use ranges: ```if value in range(min, max):``` Keep in mind that ```min``` is included in the ```range()```, but ```max``` is not. <br> ### 4) Use ```map()``` to apply a function to multiple elements and collect the results Remember the [earlier example](#1-learn-to-transform-complex-for-loops-into-comprehensions) when we were doing ```new_items = [process_item(item) for item in items]```? You can use ```map()``` to make it even more compact, by doing: ```new_items = map(process_item(item), items)``` ```map()``` will apply the ```process_item()``` function to each ```item``` in ```items``` and return the results. -
Julynx revised this gist
May 11, 2023 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -171,7 +171,7 @@ unique_cities = set(cities) print(unique_cities) # >> {'London', 'Paris'} unique_cities = list(unique_cities) print(unique_cities) # >> ['London', 'Paris'] ``` -
Julynx revised this gist
May 11, 2023 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -607,7 +607,7 @@ result = next((number for number in numbers if number > 3), None) # Fallback value. -
Julynx revised this gist
May 11, 2023 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -607,7 +607,7 @@ result = next((number for number in numbers if number > 3), None) # Fallback value. -
Julynx revised this gist
May 11, 2023 . 1 changed file with 8 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -603,14 +603,14 @@ print(result) ```python numbers = [1, 3, 4, 5, 7, 9, 2] result = next((number for number in numbers if number > 3), None) # Fallback value. print(result) # >> 4 ``` -
Julynx revised this gist
May 11, 2023 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -157,7 +157,7 @@ for city in cities: if city not in unique_cities: unique_cities.append(city) print(unique_cities) # >> ['London', 'Paris'] ``` @@ -168,11 +168,11 @@ print(cities) cities = ['London', 'Paris', 'London'] unique_cities = set(cities) print(unique_cities) # >> {'London', 'Paris'} cities = list(cities) print(unique_cities) # >> ['London', 'Paris'] ``` -
Julynx revised this gist
May 8, 2023 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -549,7 +549,7 @@ for request in requests: res = process_request(request) results.append(res) # Runs in 1m 22s, depending on HW. ``` </td> @@ -563,7 +563,7 @@ requests = [req1, req2...] with Pool as p: results = p.map(process_request, requests) # Runs in 16s, depending on HW. ``` </td> -
Julynx revised this gist
May 8, 2023 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -645,4 +645,5 @@ You can use them as conditions in your ```if``` statements. Instead of writing separate conditions (```if min < value and value < max:```): - Use intervals: ```if min < value < max:``` - Or use ranges: ```if value in range(min, max):``` Keep in mind that ```min``` is included in the ```range()```, but ```max``` is not. -
Julynx revised this gist
May 8, 2023 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -644,4 +644,5 @@ You can use them as conditions in your ```if``` statements. ### 3) Simplify your conditions with intervals or ranges Instead of writing separate conditions (```if min < value and value < max:```): - Use intervals: ```if min < value < max:``` - Or use ranges: ```if value in range(min, max):``` Notice that ```min``` is included in the ```range()``` but ```max``` is not. -
Julynx revised this gist
May 8, 2023 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -625,8 +625,6 @@ print(result) Here are some secret extra tips, for making it all the way to the end. ### 1) Assign values to multiple variables at a time or swap them - Use multiple assignments and commas to give values to more than one variable at a time: ```a, b, c = 1, 2, 3``` -
Julynx revised this gist
May 8, 2023 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -625,6 +625,8 @@ print(result) Here are some secret extra tips, for making it all the way to the end. <br> ### 1) Assign values to multiple variables at a time or swap them - Use multiple assignments and commas to give values to more than one variable at a time: ```a, b, c = 1, 2, 3``` -
Julynx revised this gist
May 8, 2023 . 1 changed file with 6 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -619,6 +619,8 @@ print(result) </tr> </table> <br> ## Extra Tips Here are some secret extra tips, for making it all the way to the end. @@ -628,13 +630,17 @@ Here are some secret extra tips, for making it all the way to the end. - Use multiple assignments and commas to give values to more than one variable at a time: ```a, b, c = 1, 2, 3``` - Or easily swap two variables in a single line: ```a, b = b, a``` <br> ### 2) Use ```all()``` and ```any()``` - ```all(a, b, c)``` returns ```True``` if a, b and c are ```True```. - ```any(a, b, c)``` returns ```True``` if at least one of a, b or c is ```True```. You can use them as conditions in your ```if``` statements. <br> ### 3) Simplify your conditions with intervals or ranges Instead of writing separate conditions (```if min < value and value < max:```): - Use intervals: ```if min < value < max:``` -
Julynx revised this gist
May 8, 2023 . No changes.There are no files selected for viewing
-
Julynx revised this gist
May 8, 2023 . 1 changed file with 4 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -635,6 +635,7 @@ Here are some secret extra tips, for making it all the way to the end. You can use them as conditions in your ```if``` statements. ### 3) Simplify your conditions with intervals or ranges Instead of writing separate conditions (```if min < value and value < max:```): - Use intervals: ```if min < value < max:``` - Or use ranges: ```if value in range(min, max):``` -
Julynx revised this gist
May 8, 2023 . 1 changed file with 7 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -625,11 +625,16 @@ Here are some secret extra tips, for making it all the way to the end. ### 1) Assign values to multiple variables at a time or swap them - Use multiple assignments and commas to give values to more than one variable at a time: ```a, b, c = 1, 2, 3``` - Or easily swap two variables in a single line: ```a, b = b, a``` ### 2) Use ```all()``` and ```any()``` - ```all(a, b, c)``` returns ```True``` if a, b and c are ```True```. - ```any(a, b, c)``` returns ```True``` if at least one of a, b or c is ```True```. You can use them as conditions in your ```if``` statements. ### 3) Simplify your conditions with intervals Use intervals (```if min < value < max:```) instead of writing separate conditions (```if min < value and value < max:```). -
Julynx revised this gist
May 8, 2023 . 1 changed file with 16 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -617,4 +617,19 @@ print(result) </td> </tr> </table> ## Extra Tips Here are some secret extra tips, for making it all the way to the end. ### 1) Assign values to multiple variables at a time or swap them - Use multiple assignment and commas to give values to more than one variable at a time: ```a, b, c = 1, 2, 3``` - Or easily swap two variables in a single line: ```a, b = b, a``` ### 2) Use ```all()``` and ```any()``` - ```all(a, b, c)``` returns ```True``` if a, b and c are ```True```. - ```any(a, b, c)``` returns ```True``` if at least one of a, b or c is ```True```. You can use them as conditions in your ```if``` statements. -
Julynx revised this gist
May 7, 2023 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -398,7 +398,7 @@ print(text) It's usually better to use comprehensions over for loops in Python when possible, as they are usually faster and shorter. However, it can be difficult to transform long and complex for loops into comprehensions. If you are iterating over ```items```, processing them in some way, and appending the results to a list to return it afterwards, try doing this instead: First, extract the body of the loop to a separate function that processes one ```item``` at a time. Let's call it ```process_item()```. Then, build your list comprehension by calling ```process_item()``` over all the ```items```. -
Julynx revised this gist
May 7, 2023 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -344,7 +344,7 @@ except OSError as exc: <br> ### 5) Use ```center``` to center strings and ```os.get_terminal_size().columns``` to center to terminal. Learn how to use ```text.ljust(length)``` to align text to the left, filling it with spaces until it reaches a desired ```length```. Use ```text.rjust(length)``` or ```text.center(length)``` to align text to the right or center respectively. -
Julynx revised this gist
May 7, 2023 . 1 changed file with 9 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -20,7 +20,7 @@ ### 2) Use ```with``` statements to automatically ```close``` files Instead of calling ```close()``` manually when you finish accessing a file, consider using a ```with``` statement. It's shorter, more readable, and less error-prone, as it will take care of closing the file automatically for you. <table> <tr> @@ -41,8 +41,8 @@ file.close() ```python with open(file_path, 'w') as file: file.write('Hello, World!') # The file is closed automatically. ``` </td> @@ -53,7 +53,7 @@ with open(file_path, 'w') as file: ### 3) Use ```f-strings``` for formatting An ```f-string``` is preceded by the letter ```f``` and allows for inserting variables between curly braces ```{}```. Using ```f-strings``` is generally faster and makes your code more readable. <table> @@ -186,7 +186,7 @@ print(cities) ### 1) Instead of ```if variable == a or variable == b``` use ```if variable in {a, b}``` Instead of repeating a variable in different conditions of an ```if``` statement, check for belonging against a set of allowed values. This is shorter, less prone to error, and makes it easier to add or remove allowed values in the future. <table> <tr> @@ -218,9 +218,9 @@ if variable in {a, b}: ### 2) Learn how to use inline ```if``` statements It is possible to write inline ```if``` statements to make assignments to variables based on a condition. Whether to use one or the other is mostly subjective and depends on which one you find more aesthetic and easier to read. Regardless, you should know and understand both in case you come across them. <table> <tr> @@ -448,7 +448,7 @@ new_items = [process_item(item) ### 2) Write readable comprehensions List, set, dictionary, and generator comprehensions may generally be shorter and run faster, but they can also be much harder to read. This is why, instead of writing your comprehensions in a single line, try splitting them into multiple lines. Notice how each keyword (```for```, ```in```, ```if```...) starts a new line. This makes the comprehension more comfortable to both read and modify if you need to add new conditions in the future. @@ -488,7 +488,7 @@ comments = {line_idx: line.strip() <br> ### 3) Don't iterate if you don't have to Iterating is expensive in Python and most common operations that require iteration can be done through functions that are either built-in or available in popular libraries/modules like ```functools```, ```itertools```, and ```numpy```. The underlying code for these functions is usually written in C and they are highly optimized. Use them whenever possible and iterate only when strictly necessary. <table> <tr> -
Julynx revised this gist
May 7, 2023 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -170,7 +170,7 @@ unique_cities = set(cities) print(cities) # >> {'London', 'Paris'} cities = list(cities) print(cities) # >> ['London', 'Paris'] -
Julynx revised this gist
May 7, 2023 . 1 changed file with 5 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -150,30 +150,30 @@ Transform your ```list``` into a ```set``` to remove duplicate elements. You can <td> ```python cities = ['London', 'Paris', 'London'] unique_cities = [] for city in cities: if city not in unique_cities: unique_cities.append(city) print(cities) # >> ['London', 'Paris'] ``` </td> <td> ```python cities = ['London', 'Paris', 'London'] unique_cities = set(cities) print(cities) # >> {'London', 'Paris'} cities = list(cities) print(cities) # >> ['London', 'Paris'] ``` </td> -
Julynx revised this gist
May 7, 2023 . 1 changed file with 47 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -135,6 +135,53 @@ print(text) <br> ### 5) Transform ```list``` into ```set``` to remove duplicates Instead of iterating over a ```list``` to remove duplicate elements, take advantage of the properties of certain data structures, like the ```set```, which can only contain distinct elements. Transform your ```list``` into a ```set``` to remove duplicate elements. You can transform it back to a ```list``` afterwards if you need to. <table> <tr> <th>Instead of doing this...</th> <th>try this:</th> </tr> <tr> <td> ```python cities = ['London', 'Paris', 'London', 'Madrid'] unique_cities = [] for city in cities: if city not in unique_cities: unique_cities.append(city) print(cities) # >> ['London', 'Paris', 'Madrid'] ``` </td> <td> ```python cities = ['London', 'Paris', 'London', 'Madrid'] unique_cities = set(cities) print(cities) # >> {'London', 'Paris', 'Madrid'} cities = list(cities) print(cities) # >> ['London', 'Paris', 'Madrid'] ``` </td> </tr> </table> <br> ## Intermediate Tips ### 1) Instead of ```if variable == a or variable == b``` use ```if variable in {a, b}```
NewerOlder