473,769 Members | 1,934 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8447
ruari mactaggart wrote:
can i write
dictionar y[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
dictionar y[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.c om...
ruari mactaggart wrote:
can i write
>dictionar y[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.pytho n responders,
the results are instant, always accurate, and
immediately available for exploring alternatives.
Raymond Hettinger
Jul 18 '05 #6

"ruari mactaggart" <ru***@charlief ortune.com> wrote in message
news:bo******** **@news7.svr.po l.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
2658
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 (with a key = a string from the list and value = None) for the purpose of insertion and removal? Basically, if Python really implements lists as linked lists but dictionaries as hash tables, it may well be that hashing a key takes negligible time...
9
2090
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
12713
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 increase. Does Python have a limit on the number of nested for-loops? Thanks.
15
2668
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
2629
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 useful for storing constants especially. Generally I find myself either needing to retrieve the values of constants in an iterative way (as in my contrived example below). Perhaps even more frequent is given one value is to look up the matching...
2
2018
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 names I've made up to illustrate the point more clearly. (the embedded device device can't produce XML and this is what I have
1
4585
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 to store persistent information between script executions. The following code does not work for me, import shelve, sys, os, string db = shelve.open(os.path.abspath(os.path.dirname(sys.argv)) + '/' + 'sandbox.dat', 'c') db = 'apple'
9
1267
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 in the right direction. This should be pretty simple: I have two dictionaries, foo and bar. I am certain that all keys in bar belong to foo as well, but I also know that not all keys in foo exist in bar. All the keys in both foo and bar are...
14
1820
by: cnb | last post by:
Are dictionaries the same as hashtables?
0
9579
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, 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...
0
9420
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,...
0
10035
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8863
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7401
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6662
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();...
0
5441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3949
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
3
2811
bsmnconsultancy
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...

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.