473,395 Members | 1,462 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Python:Query on Conditional Loops and the setdefault method

3
>>>listoflists = [['a', 'first'], ['b', 'second'], ['a', 'next'], ['c', 'third'], ['b', 'last']]
>>> dd = {}
>>>for item in listoflists:
... dd.setdefault(item[0], []).append(item)

- The part which mentions 'item[0]' would be to have the first letter of each item in the list we created right ?
- what does the ' [ ] ' signify in this case ? Does it mean to have a null value for each key that we create for the dictionary and add/create the value associated with the key from the append method ?

Regards,
Sudip Bhattacharya
Feb 21 '10 #1
1 1611
Motoma
3,237 Expert 2GB
You are correct, in this case, item[0] represents 'a', 'b', or 'c'.

[] signifies an empty list. If you look at the documentation for setdefault, it will return a the value of the dictionary element specified in the first argument, or, if it has no value, it will return the second argument.
Feb 21 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Akhilesh S. Shirbhate | last post by:
Sorry for reposting, but please help me... I desperately need help. I wrote a small CORBA program and am using ORBit-python. The IDL file was as below: --------------------------- module...
3
by: evelyne0510 | last post by:
Hi all, I have created a XML-RPC model (with server and client) written in Java. I want to call the methods in another XML-RPC model written in Python. I know that in Java, I can use like...
2
by: Andrew Robert | last post by:
I have two Perl expressions If windows: perl -ple "s/()/sprintf(q#%%%2X#, ord $1)/ge" somefile.txt If posix perl -ple 's/()/sprintf("%%%2X", ord $1)/ge' somefile.txt
10
by: Putty | last post by:
In C and C++ and Java, the 'for' statement is a shortcut to make very concise loops. In python, 'for' iterates over elements in a sequence. Is there a way to do this in python that's more concise...
5
by: johnny | last post by:
Is there a way to call a function on a specified interval(seconds, milliseconds) every time, like polling user defined method? Thanks.
3
by: Allen | last post by:
I use try catch, but cannot catch the execeptions of execution python method. PYCALL_API void PyCall(const char * pszModule, const char * pszFunc, void * pArg) { if (pszModule == NULL ||...
0
by: arunbuddha | last post by:
Hi All, I am a newbie to Python programming. I am interfacing python program with c++ shared object library using swig and facing a problem with callback methods. C++ method : file1.h class...
0
by: SamShak | last post by:
I'm relatively new to python. I'm trying to debug the below mentioned problem. Can someone please help me with this? ------------------------------- from ctypes import * class...
0
by: iaminlost | last post by:
for example. in python : def MyMethod(self): print "MyMethod" window.external.Mythod() i want this javascript language can call the python method, what else should i do?
0
by: =?UTF-8?B?TmlscyBPbGl2ZXIgS3LDtmdlcg==?= | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I don't think the global interpreter lock is what you need ... read here for reference: http://docs.python.org/api/threads.html
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
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...
0
Oralloy
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.