About 91,300 results
Open links in new tab
  1. python - Use 'try/except' instead of 'if' to find duplicates in a list ...

    Jan 5, 2017 · I'm making a program that finds item that are repeated in an array and I did have it working using the if command, but with long arrays the if command slows down the program so I wish to convent it to the try/except command. Here's my code: if item not in seen: seen.append(item) else: repeats.append(item) Output: [11, 4] thanks.

  2. Try/Except in Python: How to avoid duplication?

    Sep 1, 2017 · In this example, the ApiWrapper class converts the low-level ApiException into the nicer ApiWrapperException. But there's a lot of duplication. I can put the duplicated code in an inner function, like this: def call(*args, **kwargs): try: return api_callable(*args, **kwargs) except ApiException: print 'ApiWrapper caught an ApiException.

  3. How to Find Duplicates in a List – Python | GeeksforGeeks

    Nov 27, 2024 · Removing duplicates from a list is a common operation in Python which is useful in scenarios where unique elements are required. Python provides multiple methods to achieve this. Using set() method is most efficient for unordered lists.

  4. Ways to remove duplicates from list in Python - GeeksforGeeks

    Nov 18, 2024 · In this article, we’ll learn several ways to remove duplicates from a list in Python. The simplest way to remove duplicates is by converting a list to a set. We can use set () to remove duplicates from the list. However, this approach does not preserve the original order.

  5. check for duplicates in a python list - Stack Overflow

    If you simply want to check if it contains duplicates. Once the function finds an element that occurs more than once, it returns as a duplicate. my_list = [1, 2, 2, 3, 4] def check_list(arg): for i in arg: if arg.count(i) > 1: return 'Duplicate' print check_list(my_list) == 'Duplicate' # prints True

  6. Python Try Except: Handling Errors Like a Pro | Python Central

    When you're coding in Python, understanding the try-except mechanism is crucial for handling errors effectively. The try block lets you define code where exceptions might occur, while the except block specifies how to handle errors, preventing crashes.

  7. Exception & Error Handling in Python - Codecademy

    Mar 19, 2025 · Types of errors in Python. Python categorizes errors into three main types: 1. Syntax errors. These errors arise when the code violates Python’s syntax rules. The interpreter usually points them out during compilation, making them easy to spot. For example:

  8. Removing Duplicates from Lists in Python - CodeRivers

    2 days ago · In Python programming, dealing with data often involves working with lists. Lists are a versatile data structure that can store various types of elements. However, it's common to encounter situations where a list contains duplicate elements. Removing these duplicates is a fundamental operation that can be crucial for data analysis, data cleaning, and ensuring the integrity of algorithms.

  9. Finding Duplicates in Python Lists: A Complete Guide

    Nov 4, 2024 · Here are three common approaches to find duplicates in a list: seen = set() duplicates = set() duplicates.add(item) seen.add(item) count_dict = {} count_dict[item] = count_dict.get(item, 0) +...

  10. How to Find Duplicates in a Python List? - Python Guides

    Mar 4, 2025 · Learn how to find duplicates in a Python list using loops, `collections.Counter()`, and `set()`. This guide includes step-by-step examples for easy understanding.

  11. Some results have been removed
Refresh