About 104,000 results
Open links in new tab
  1. Use curly braces to initialize a Set in Python - Stack Overflow

    Curly braces or the set() function can be used to create sets. Note: to create an empty set you have to use set(), not {}; the latter creates an empty dictionary, a data structure that we discuss in the next section. in python 2.7: >>> my_set = {'foo', …

  2. How do I properly initialize a set in Python? - Stack Overflow

    Feb 12, 2021 · I initially defined a set using destinations={"x",}. Then I tried to remove something using destinations=destinations.discard("x"). However, the terminal says AttributeError: 'NoneType' object has no attribute 'discard' when I try to run it. It seems that it is not yet a set.

  3. python - Creating an empty set - Stack Overflow

    A set literal is just a tuple of values in curly braces: x = {2, 3, 5, 7} So, you can create an empty set with empty tuple of values in curly braces: x = {*()} Still, just because you can, doesn't mean you should. Unless it's an obfuscated programming, or a codegolf where every character matters, I'd suggest an explicit x = set() instead.

  4. How do I create a Python set with only one element?

    Aug 12, 2015 · The set() builtin expects an iterable type argument. In python, strings can be treated as iterable objects where each character is an element. Attempting to create a set("my_string") will instead create a string of its constituent characters. To properly create a set using the builtin function we wrap it in a list here. –

  5. Initialize a string variable in Python: "" or None?

    None is used to indicate "not set", whereas any other value is used to indicate a "default" value. Hence, if your class copes with empty strings and you like it as a default value, use "". If your class needs to check if the variable was set at all, use None. Notice that it doesn't matter if your variable is a string initially.

  6. python - What's the Pythonic way to initialize, set and get my …

    In Python 3.7+ there is the data class library. This library will allow you to create your own class to hold data quickly using a decorator, @dataclass . The @dataclass decorator allows you to quickly define and add functionality to a class you intend to mostly be used to hold data.

  7. python - More elegant way of declaring multiple variables at the …

    Apparently Python 2.7 has dictionary comprehensions, which would make for an extremely concise way to implement that function. This is left as an exercise to the reader, since I don't have Python 2.7 installed :) You can also combine some functions from the ever-versatile itertools module. As they say, There's More Than One Way To Do It. Wait ...

  8. "Initializing" variables in python? - Stack Overflow

    Jun 16, 2015 · In python, if the left hand side of the assignment operator has multiple variables, python would try to iterate the right hand side that many times and assign each iterated value to each variable sequentially. The variables grade_1, grade_2, grade_3, average need three 0.0 values to assign to each variable. You may need something like -

  9. Python initialize multiple variables to the same initial value

    Oct 25, 2015 · Python assigning multiple variables to same value? list behavior concerned with tuples, I want just variables may be a string, integer or dictionary More elegant way of declaring multiple variables at the same time

  10. How to initialize a set and print an empty set {} in python?

    Sep 23, 2019 · How do I initialize and print a set such that if it is empty then it should show {} and not set() Example: If the set has contents, then it gets printed as {1,2,3} but if empty then I want it to show as {} (and not as set() ).

Refresh