Hi,
Is there an equivalent keyword argument syntax for these cases:
d1 = {1:"one"}
d2 = {"1":"one"}
these don't work, of course: a = dict(1 = "one")
SyntaxError: keyword can't be an expression
a = dict("1" = "one")
SyntaxError: keyword can't be an expression
Just curious... 2 5068
"Oktay Safak" <ok********@ixi r.com> wrote in message
news:72******** *************** ***@posting.goo gle.com... Hi,
Is there an equivalent keyword argument syntax for these cases:
d1 = {1:"one"}
d2 = {"1":"one"}
these don't work, of course:
a = dict(1 = "one") SyntaxError: keyword can't be an expression a = dict("1" = "one")
SyntaxError: keyword can't be an expression
As you discovered, there is no equivalent. The new
syntax is limited to strings that are valid identifiers
(variable names).
Raymond Hettinger
> > Hi, Is there an equivalent keyword argument syntax for these cases:
d1 = {1:"one"}
d2 = {"1":"one"}
these don't work, of course:
>> a = dict(1 = "one") SyntaxError: keyword can't be an expression
>> a = dict("1" = "one") SyntaxError: keyword can't be an expression
As you discovered, there is no equivalent. The new syntax is limited to strings that are valid identifiers (variable names).
Raymond Hettinger
Thanks :) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Robin Cull |
last post by:
Imagine I have a dict looking something like this:
myDict = {"key 1": , "key
2": , "key 3": ,
"key 4": }
That is, a set of keys which have a variable length list of associated
values after them. What I want to do is filter out a subset of this
dict to produce another dict that satisfies a set of criteria (in this
case whether it contains all four values) to end up with something
|
by: Steven Bethard |
last post by:
I'd like to subclass dict to disallow overwriting of keys, something like:
>>> class SafeDict(dict):
.... def __setitem__(self, key, value):
.... if key in self:
.... raise KeyError('cannot assign value %r to key %r, value %r'
.... 'already exists' % (value, key, self))
.... super(SafeDict, self).__setitem__(key, value)
....
|
by: cheng |
last post by:
hi all..it a problem about dict:
print target, dict
get output:
keyword
{page3.html, page2.html, page1.html}
is it some ways to change it to:
|
by: talin at acm dot org |
last post by:
Although I realize the perils of even suggesting polluting the Python
namespace with a new keyword, I often think that it would be useful to
consider defining an operator for testing whether or not an item is a
member of a category.
Currently, we have the 'in' operator, which tests for membership within
a container, and that works very well -- in particular, it allows such
membership tests to be expressed in very natural way. So for...
|
by: Alex |
last post by:
Entering
>>> help(dict)
Help on class dict in module __builtin__:
class dict(object)
| dict() -> new empty dictionary.
| dict(mapping) -> new dictionary initialized from a mapping object's
| (key, value) pairs.
| dict(seq) -> new dictionary initialized as if via:
| |
by: David Rasmussen |
last post by:
If I have a collection of dicts like:
john = {'id': 1, 'name': "John Cleese", 'year': 1939}
graham = {'id': 2, 'name': "Graham Chapman", 'year': 1941}
I could store all of them in a list. But for easy lookup, I might store
all these in a dict instead, like
people = {'1': john, '2': graham}
|
by: Kamilche |
last post by:
Hi everyone. I'm trying to convert a string that looks like this:
gid = 'FPS', type = 'Label', pos = , text = 'FPS', text2 = 'more
text without quotes', fmtline = "@VALUE @SIGNAL", signals =
to a dict that looks like this:
{'signals': , 'text': 'FPS',
'pos': , 'text2': 'more text without quotes', 'gid': 'FPS',
'type': 'Label', 'fmtline': '@VALUE @SIGNAL'}
|
by: George Sakkis |
last post by:
Although I consider dict(**kwds) as one of the few unfortunate design
choices in python since it prevents the future addition of useful
keyword arguments (e.g a default value or an orderby function), I've
been finding myself lately using it sometimes instead of dict literals,
for no particular reason. Is there any coding style consensus on when
should dict literals be preferred over dict(**kwds) and vice versa ?
George
|
by: bearophileHUGS |
last post by:
I'd like to know why Python 2.6 doesn't have the syntax to create sets/
dicts of Python 3.0, like:
{x*x for x in xrange(10)}
{x:x*x for x in xrange(10)}
Bye,
bearophile
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |