1

I have the following list of lists:

MyList = [[130, 10], [131, 15], [132, 1]]

Then i have some inputs. If i get, for example: Data = [130, 3], [135, 10], i need to update the list like this:

MyList = [[130, 3], [131, 15], [132, 1], [135, 10]]

So, if in MyList there is already a sublist where the first element is the same as the first element of a sublist in Data, update the same element. Instead, if there isn't one, append it.

I managed to do this but i was wondering if there was a cleaner solution, as i really don't like the actual one:

Temp = [x[0] for x in MyList]

for x in Data:
    if x[0] not in Temp:
        Sublist = []
        Sublist.append(x[0])
        Sublist.append(x[1])
        MyList.append(Sublist)
    else:
        for y in MyList:
            if x[0] == y[0]:
                x[1] = y[1]

Is there any better way to do this? I feel like this code can be improved, i also don't like editing elements while looping. Any kind of help is welcome!

4
  • I think the code you have is the most efficient it can be, sorry for the unhelpfulness Commented Jun 18, 2020 at 20:04
  • 4
    If this is working code that you think could be improved, see Code Review. If not, please clarify the problem with a minimal reproducible example. Commented Jun 18, 2020 at 20:05
  • 3
    you could avoid problems if dict was used Commented Jun 18, 2020 at 20:07
  • A cleaner solution would be to not use a list, probably better of with a dict. Commented Jun 18, 2020 at 20:13

4 Answers 4

4

The cleanest solution would be to use a dict to begin with. So,

>>> data = dict([[130, 10], [131, 15], [132, 1]])
>>> data
{130: 10, 131: 15, 132: 1}
>>> for x, y in [130, 3], [135, 10]:
...     data[x] = y
...
>>> data
{130: 3, 131: 15, 132: 1, 135: 10}

If you really need a list at the end:

>>> list(data.items())
[(130, 3), (131, 15), (132, 1), (135, 10)]

Or even just:

>>> data = {130: 10, 131: 15, 132: 1}
>>> new_data = [130, 3], [135, 10]
>>> data.update(new_data)
>>> data
{130: 3, 131: 15, 132: 1, 135: 10}
Sign up to request clarification or add additional context in comments.

3 Comments

i think that, indeed, using a dict is the best way to go here, rather than a list. Thank you!
@Jack022 yes, one of the most important considerations for clean code is to choose the right data structures that allow you to write the code cleanly in the first place
One last thing: wouldn't i get the same output here using the update() method?
0

If I got your problem right, this is what I have in mind, similar to yours.

myList = [[130, 10], [131, 15], [132, 1]]
data = [130, 3], [135, 10]

if data[0][0] == myList[0][0]:
    myList.pop(0)
    myList.insert(0, data[0])
    myList = myList + list(data[1:])
else:
    myList = myList + list(data)

print(myList)

output:

[[130, 3], [131, 15], [132, 1], [135, 10]]

Comments

0

A cleaner solution (if you insist to use lists instead of the preferred structure dictionary) might be:

MyList = [item for item in MyList if item[0] not in [i[0] for i in Data]]
MyList.extend(Data)

Comments

0

As per your this question, what you can do is extend your main list with all the data, and then use dict operation to make a dictionary

MyList = [[130, 10], [131, 15], [132, 1]]


new_data = [[130, 3], [135, 10]]

MyList.extend(new_data)

final_data=  dict(MyList)
print(final_data)
# {130: 3, 131: 15, 132: 1, 135: 10}

1 Comment

this isn't a clean way to use a dict at all. There's no need to check if a in my_dic.keys(), and even if there were, you should use if x in my_dict. In any case, that is pointless, because you always do my_dic.update({a:b}), which is also not a clean way of doing that, that should just be my_dic[a] = b

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.