473,385 Members | 2,269 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,385 software developers and data experts.

dictionaries with nested lists

can i write
dictionary[key][list][3]


to mean the 3rd item in the list that is the corresponding value for 'key' ?
Jul 18 '05 #1
6 8430
ruari mactaggart wrote:
can i write
dictionary[key][list][3]


to mean the 3rd item in the list that is the corresponding value for 'key'
?


No, but you can write dictionary[key][2] for that purpose:

-- that[list] notation is unneeded and unsupported,
-- indices start from 0 so the 3rd one is indexed as [2]
Alex

Jul 18 '05 #2
ruari mactaggart wrote:
can i write
dictionary[key][list][3]


to mean the 3rd item in the list that is the corresponding value for 'key' ?


You don't need the[list] part. Assuming you mean something like:

bands = {'beatles': ['john','paul','george','ringo']}

....then bands['beatles'][3] == 'ringo'

Peace,
Joe

Jul 18 '05 #3
what if i mean
bands={'beatles':['john','paul','george','ringo'],['guitar','bass','guitar',
'drums']}

then i need to put bands['beatles'][1][3]='percussion'

i presume... ?

Joe Francia <us****@soraia.com> wrote in message
news:Jn**********************@news.easynews.com...
ruari mactaggart wrote:
can i write
>dictionary[key][list][3]
to mean the 3rd item in the list that is the corresponding value for

'key' ?
You don't need the[list] part. Assuming you mean something like:

bands = {'beatles': ['john','paul','george','ringo']}

...then bands['beatles'][3] == 'ringo'

Peace,
Joe

Jul 18 '05 #4
ruari mactaggart wrote:
what if i mean
bands={'beatles':['john','paul','george','ringo'],['guitar','bass','guitar', 'drums']}
Invalid syntax.
then i need to put bands['beatles'][1][3]='percussion'


If you mean that bands is a dictionaries of tuples of lists, yes, but you
need parentheses around the tuples (and I would suggest some formatting
for greater readability), e.g.:

bands = {'beatles': ( ['john', 'paul', 'george', 'ringo'],
['guitar', 'bass', 'guitar', 'drums'] )
}
Alex

Jul 18 '05 #5
[ruari mactaggart]
what if i mean
bands={'beatles':['john','paul','george','ringo'],['guitar','bass','guitar',
'drums']}

then i need to put bands['beatles'][1][3]='percussion'

i presume... ?


No need to presume.
Just fire up the interpreter and see.

Compared to comp.lang.python responders,
the results are instant, always accurate, and
immediately available for exploring alternatives.
Raymond Hettinger
Jul 18 '05 #6

"ruari mactaggart" <ru***@charliefortune.com> wrote in message
news:bo**********@news7.svr.pol.co.uk...
what if i mean
bands={'beatles':['john','paul','george','ringo'],['guitar','bass','guitar', 'drums']}

then i need to put bands['beatles'][1][3]='percussion'

i presume... ?


If things are getting this complicated, you're probably better off a)
wrapping an interface to this dict using an object, or b) just using object
attributes and using those objects as dictionary items.

Oh, and you need to group those two lists into a single list or tuple....
--
Francis Avila

Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Narendra C. Tulpule | last post by:
Hi, if you know the Python internals, here is a newbie question for you. If I have a list with 100 elements, each element being a long string, is it more efficient to maintain it as a dictionary...
9
by: T. Earle | last post by:
To list, I'm trying to figure out the best approach to the following problem: I have four variables: 1) headlines 2) times 3) states 4) zones
25
by: chad | last post by:
I am writing a program to do some reliability calculations that require several nested for-loops. However, I believe that as the models become more complex, the number of required for-loops will...
15
by: Xah Lee | last post by:
Here's the belated Java solution. import java.util.List; import java.util.ArrayList; import java.lang.Math; class math { public static List range(double n) { return range(1,n,1); }
2
by: David Pratt | last post by:
Hi. I like working with lists of dictionaries since order is preserved in a list when I want order and the dictionaries make it explicit what I have got inside them. I find this combination very...
2
by: techiepundit | last post by:
I'm parsing some data of the form: OuterName1 InnerName1=5,InnerName2=7,InnerName3=34; OuterName2 InnerNameX=43,InnerNameY=67,InnerName3=21; OuterName3 .... and so on.... These are fake...
1
by: Matthew Schibler | last post by:
I'm a newbie to Python, with some experience using perl (where I used nested arrays and hashes extensively). I am building a script in python for a MUD I play, and I want to use the shelve module...
9
by: Brandon | last post by:
Hi all, I am not altogether experienced in Python, but I haven't been able to find a good example of the syntax that I'm looking for in any tutorial that I've seen. Hope somebody can point me...
14
by: cnb | last post by:
Are dictionaries the same as hashtables?
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.