in Education by
What is the best way to update a value in a list of tuples? I currently do it like in the code below, but I suppose there is a cleaner and concise way. >>> foo = [('a', 'hello'), ('b', 'world')] >>> bar = dict(foo) >>> bar['b'] = 'friend' >>> foo = bar.items() >>> foo [('a', 'hello'), ('b', 'friend')] Edit: The reason to use a list of tuple wasn't clear in my original post. The goal is to update some headers values of a wsgi application during error handling, that are a list of tuples. Thanks in advance. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Your data structure (a list of tuple) is best referred as an associative list. As other pointed out, it is probably better to use a dictionary as you'll get better amortized cost on operation (insertion, deletion, and lookup are O(1) for a dictionary, but deletion and lookup are O(n) for associative list). Concerning updating your associative list by converting it to a dictionary, and then back to an associative list, this method has three drawbacks. It is quite expensive, it may change the order of the items, and it will remove duplicate. If you want to keep using associative lists, it is probably better to just use a list comprehension to update the data structure. The cost will be O(n) in time and memory, but that's already what you have when using an intermediate dictionary. Here's a simple way to do it (require Python 2.5 because it use the ternary operator): def update_in_alist(alist, key, value): return [(k,v) if (k != key) else (key, value) for (k, v) in alist] def update_in_alist_inplace(alist, key, value): alist[:] = update_in_alist(alist, key, value) >>> update_in_alist([('a', 'hello'), ('b', 'world')], 'b', 'friend') [('a', 'hello'), ('b', 'friend')]

Related questions

0 votes
    What is the best way to update a value in a list of tuples? I currently do it like in the code ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 7, 2022 in Education by JackTerrance
0 votes
    What is the best way to update a value in a list of tuples? I currently do it like in the code ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 7, 2022 in Education by JackTerrance
0 votes
    What is the best way to update a value in a list of tuples? I currently do it like in the code ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 5, 2022 in Education by JackTerrance
0 votes
    What is the best way to update a value in a list of tuples? I currently do it like in the code ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 5, 2022 in Education by JackTerrance
0 votes
    What is the best way to update a value in a list of tuples? I currently do it like in the code ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 5, 2022 in Education by JackTerrance
0 votes
    I have some data either in a list of lists or a list of tuples, like this: data = [[1,2,3], [4,5, ... store tuples or lists in my list? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    I have the list of sets like so. I basically want to convert this to the dictionary and to address duplicate keys, ... ] = val return d Select the correct answer from above options...
asked Jan 9, 2022 in Education by JackTerrance
0 votes
    How will you create a dictionary using tuples in python?...
asked Nov 24, 2020 in Technology by JackTerrance
0 votes
    What is the difference between tuples and lists in Python?...
asked Nov 23, 2020 in Technology by JackTerrance
0 votes
    What are tuples in Python?...
asked Nov 23, 2020 in Technology by JackTerrance
0 votes
    I have the following dictonary: inventory = {'apple':(3, 133), 'banana':(9, 407), 'pear':( ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 3, 2022 in Education by JackTerrance
0 votes
    I have the following dictonary: inventory = {'apple':(3, 133), 'banana':(9, 407), 'pear':( ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I have the following dictonary: inventory = {'apple':(3, 133), 'banana':(9, 407), 'pear':( ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    Write a program in python to insert ten friends name in a list, then print 3rd, 5th and 9th value from the list (using data structure) Select the correct answer from above options...
asked Dec 14, 2021 in Education by JackTerrance
0 votes
    What will be the output of the following Python program? z=set('abc') z.add('san') z.update(set(['p', 'q'] ... ;, ‘c’, [‘p’, ‘q’], ‘san}...
asked Jan 2, 2023 in Technology by JackTerrance
...