Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

A list of data structures in Python

I am pretty new to Python (coming from a Java background) so was wondering if someone would have any advice on a data structure design question. I need to create a data structure with default values that would look something like this:

[
(Name="name1", {id=1, val1="val1"} ),
(Name="name2", {id=2, val1="val2"} )
]

i.e a list of tuples where each tuple consists of one string value (Name) and a dictionary of values.

The first piece of functionality I need is to be able to add to or override the above data structure with additional details e.g:

[
(Name="name2", {id=2, val1="new value"} ) ,
(Name="name2", {id=3, val1="another value"} ) ,
(Name="name3", {id=3, val1="val3"} )
]

Which would ultimately result in a final data structure that looks like this:

[
(Name="name1", {id=1, val1="val1"} ),
(Name="name2", {id=2, val1="new value"} ) ,
(Name="name2", {id=3, val1="another value"} ) ,
(Name="name3", {id=3, val1="val3"} )
]

The second piece of functionality I need is to be able to access each tuple in the list according to the id value in the dictionary i.e

Get me tuple where name = "name2" and id="3" .

Could anybody give me their opinions on how best this could be implemented in Python?

Thanks!

Answer*

Draft saved
Draft discarded
Cancel

lang-py

AltStyle によって変換されたページ (->オリジナル) /