views:

261

answers:

8

Is it possible in python to add a tuple as a value in a dictionary?
And if it is,how can we add a new value, then? And how can we remove and change it?

+10  A: 
>>> a = {'tuple': (23, 32)}
>>> a
{'tuple': (23, 32)}
>>> a['tuple'] = (42, 24)
>>> a
{'tuple': (42, 24)}
>>> del a['tuple']
>>> a
{}

if you meant to use tuples as keys you could do:

>>> b = {(23, 32): 'tuple as key'}
>>> b
{(23, 32): 'tuple as key'}
>>> b[23, 32] = 42
>>> b
{(23, 32): 42}

Generally speaking there is nothing specific about tuples being in dictionary, they keep behaving as tuples.

SilentGhost
+1  A: 

You can't change a tuple itself. You have to replace it by a different tuple.

When you use a list, you could also add values to it (changing the list itself) without need to replace it:

>> a = {'list': (23, 32)}
>> a
{'list': [23, 32]}
>> a['list'].append(99)
>> a
{'list': [23, 32, 99]}

In most cases, lists can be used as replacement for tuples (since as much I know they support all tuple functions -- this is duck typing, man!)

Juergen
you seem to be missing some parts
SilentGhost
@SilentGhost: don't be shy, tell it to me! BTW: I did not intent to give a full answer, since you already gave some good advice. I only wanted to add information about lists.
Juergen
+1  A: 
Stephan202
A: 

Well,thanx for your answers...but i didnt really mean that! If we have a dictionary, a number as its key and a tuple as the value. The tuple is on this way:

t1=('name','date')

If i want to add for example a new name on the tuple...in order to put it on the dict... How can i make it?

d1={Number:'(name,date)'}

Thanxxx in advance

FILIaS
Please update the question, and do not post the update as a comment. That way it's less confusing.
extraneon
A: 
t1=('name','date')
t2=('x','y')

# "Number" is a String key!
d1={"Number":t1}
# Update the value of "Number"
d1["Number"] = t2

# Use a tuple as key, and another tuple as value
d1[t1] = t2

# Obtain values (getters)

# Can throw a KeyError if "Number" not a key
foo = d1["Number"]

# Does not throw a key error, t1 is the value if "Number" is not in the dict
d1.get("Number", t1)

# t3 now is the same as t1
t3 = d1[ ('name', 'date') ]

You updated your question again. Please take a look at Python dict docs. Python documentation is one of it's strong points! And play with the interpreter (python)on the command line! But let's continue.

  1. initially key 0

    d[0] = ('name', datetime.now())

  2. id known d[1] = d[0] del d[0]

  3. name changed tmp = d[1] d[1] = ( newname, tmp[1] )

And please consider using a

class Person(object):
    personIdCounter = 1
    def __init__(self):
      self.id = Person.personIdCounter
      Person.personIdCounter += 1
      self.name
      self.date

then

persons = {}
person = Person()
persons[person.id] = person
person.name = "something"

persons[1].name = "something else"

That looks better than a tuple and models your data better.

extraneon
thank u very much!But u know...i;m a beginner in python..and i dont really "get" your answer! I prefer ,in my first steps in python to do it in the easy way ;(i feel silly...but....:(
FILIaS
I figured that out. This is all stuff you can type on the Python command line, for you to play with. The Person class shows you an alternative way, more structured, to solve this kind of problems.
extraneon
A: 

The question is that: i want a dictionary with the basic infos of some people lets say. The dict contains numbers as keys and tuples as values.Tuples contain the name and the date for every person. I wann fix a menu choice in which: 1)i can add in the dict a new person(with initional date 0) 2)i can remove a person from the dict (using only his number) 3)i can add a new date(again using only his number) 4)and finally print the dict!

t1=('name','date')
d1={Number:'(name,date)'}
FILIaS
This should also be moved to the question.
Andrew Grimm
A: 

The question is that: i want a dictionary with the basic infos of some people lets say. The dict contains numbers as keys and tuples as values.Tuples contain the name and the date for every person. I wann fix a menu choice in which: 1)i can add in the dict a new person(with initional date 0) 2)i can remove a person from the dict (using only his number) 3)i can add a new date(again using only his number) 4)and finally print the dict!

t1=('name','date')
d1={Number:'(name,date)'}
FILIaS
A: 

I was thinking sth like that (the specific doesnt work :ppp):

names=[]                     
dates=[]                   
Number=[]                       

t1=tuple()
t1=(names) + (dates)       

d1=dict((Number)='t1')             


def new_person(name,date,Number1):

    names.append('name')
    print(names)
    grade.append('date')
    print(dates)
    t1=(names) + (dates)
    print(t1)
FILIaS
You seem to be having some very basic issues with python syntax. Have you worked your way through the tutorial? http://docs.python.org/tutorial/
jcdyer
:/ :( i know that's wrong...yes i have read the tutorial -until dictionaries :/ - thnx anyway...
FILIaS