About 545,000 results
Open links in new tab
  1. python - if/else in a list comprehension - Stack Overflow

    Since a list comprehension creates a list, it shouldn't be used if creating a list is not the goal; it shouldn't be used simply to write a one-line for-loop; so refrain from writing [print(x) for x in range(5)] for example.

  2. Python: list of lists - Stack Overflow

    The second, list(), is using the actual list type constructor to create a new list which has contents equal to the first list. (I didn't use it in the first example because you were overwriting that name in your …

  3. How do I concatenate two lists in Python? - Stack Overflow

    joined_list = [item for list_ in [list_one, list_two] for item in list_] It has all the advantages of the newest approach of using Additional Unpacking Generalizations - i.e. you can concatenate an arbitrary …

  4. python - Find a value in a list - Stack Overflow

    In Python 3, filter doesn't return a list, but a generator-like object. Finding the first occurrence If you only want the first thing that matches a condition (but you don't know what it is yet), it's fine to use a for …

  5. python - Insert an element at a specific index in a list and return the ...

    Here's the timeit comparison of all the answers with list of 1000 elements on Python 3.9.1 and Python 2.7.16. Answers are listed in the order of performance for both the Python versions.

  6. slice - How slicing in Python works - Stack Overflow

    What really annoys me is that python says that when you don't set the start and the end, they default to 0 and the length of sequence. So, in theory, when you use "abcdef" [::-1] it should be transformed to …

  7. python - How to concatenate (join) items in a list to a single string ...

    Sep 17, 2012 · For handling a few strings in separate variables, see How do I append one string to another in Python?. For the opposite process - creating a list from a string - see How do I split a …

  8. python - How can I pass a list as a command-line argument with …

    Don't use quotes on the command line 1 Don't use type=list, as it will return a list of lists This happens because under the hood argparse uses the value of type to coerce each individual given argument …

  9. python - How do I make a flat list out of a list of lists? - Stack Overflow

    If your list of lists comes from a nested list comprehension, the problem can be solved more simply/directly by fixing the comprehension; please see How can I get a flat result from a list …

  10. python - Why does "example = list (...)" result in "TypeError: 'list ...

    This means you can no longer use the predefined value of list, which is a class object representing Python list. Thus, when you tried to use the list class to create a new list from a range object: