0

This is a very basic example. I want to keep a list of variable/flags but i know that lists are immutable. In the example below, I could simply append a new tuple to hasmoustache (in a log type way) but the list hasmoustache would keep on expanding.

I have 3 questions: How to replace an element in one of the tuples in a list (see comment in the example below) - like changing the bole an in the tuples containing sam and sarah in hasmoustache? Is it good practice knowing that list are immutable? Is there another/cleaner way to keep a finite list of flag?

hasmoustache=[('jon',False,'Male'),('sam',False,'Male'),('sarah',False,'Female')]
# hasmoustache is list of tuples describing (name,has a moustache?,gender)

name = 'Joe'
gender = 'Male'
if name not in hasmoustache:
    append hasmoustache.((name,False,gender))

for y in hasmoustache:
    print y

barber=[('jon',1),('sam',8),('sarah',10)]
# barber is a list of tuples describing (name,number of weeks since last visit to the barber)
for m in barber:
    if m[1]>4
        # Do something to change tuple with name=m[0] in hasmoustache to True
        # List in python are immutable so how to do it?

callthem = [x[0] for x in hasmoustache if x[1]]

for y in callthem:
    print y

for y in hasmoustache:
    print y

Output should show: ('jon', False, 'Male') ('sam', False,'Male') ('sarah', False, 'Female') ('Joe', False, 'Male') sam sarah ('jon', False, 'Male') ('sam', True, 'Male') ('sarah', True, 'Female') ('Joe', False, 'Male')

2
  • 3
    It sounds like it's time for you to explore classes and attributes. You can have instances that carry state.
    – g.d.d.c
    May 25, 2014 at 7:17
  • Yes, absolutely. Trying to get my head around it. Shall i put the entire script here? Jun 7, 2014 at 17:17

1 Answer 1

1

You have it a little backwards. Lists are mutable, meaning you can modify their values after they are created, Tuples are immutable sequence types: such objects cannot be modified once created. If you wanted to have a structure to achieve what you want, you can use a list of lists.

barber=[['jon',1],['sam',8],['sarah',10]]

mutable sequence types

So the answer to question 1.

You cannot change the elements of a tuple so without creating a completely new tuple and replacing the current tuple in the list with that tuple you cannot do what you expect.

Question 2.

Lists are not immutable so that is non event.

Question 3.

You could use a dict using names as keys if they will all be unique and keep a list of information as values.

This is an example of using a dict:

barber={'jon': 1,'sam':8,'sarah': 10 }

if barber.get("sam")  > 4: # get value of sam
   print barber.get("sam")     
8

Make hasmoustace a dict also:

hasmoustache={'jon':[False,'Male'],'sam':[False,'Male'],'sarah':[False,'Female']}
barber={'jon':1,'sam':8,'sarah':10}

if barber.get("sam")  > 4:
   hasmoustache.get("sam")[0]=  True
print hasmoustache
{'sarah': [False, 'Female'], 'sam': [True, 'Male'], 'jon': [False, 'Male']}
1
  • Should update the boolean in hasmoustache not in barber though. barber list doesn't change. May 25, 2014 at 8:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.