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?
views:
261answers:
8>>> 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.
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!)
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
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.
initially key 0
d[0] = ('name', datetime.now())
id known d[1] = d[0] del d[0]
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.
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)'}
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)'}
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)