0

Afraid this might be a duplicate but I've tried the other answers and they do not work for me

bin1 = {'05':175, '10':185, '15':195}

bin2 = {'05':174, '10':184, '15':194}

and I want this:

binList = ({'05':175, '10':185, '15':195},{'05':174, '10':184, '15':194})

I have tried .append and .update - neither works for me

binList = bin1.update(bin2)

doesnt give anything (ie, nothing happens) and

binList = bin1.append(bin2)

errors with 'AttributeError: 'dict' object has no attribute 'append'

I suspect I just dont know enough about syntax

0

1 Answer 1

1

As simple as:

binList = [bin1,bin2]

You call this variable binList, although in your question you asked for a tuple. this can be achieved with:

binTuple = (bin1,bin2)
7
  • what's the difference with binList = (bin1, bin2) ?
    – Ed_
    Jun 26, 2015 at 20:02
  • @WeaponX Tuples are immutable in python - read more here and [here] (tutorialspoint.com/python/python_tuples.htm) Jun 26, 2015 at 20:06
  • i will actually be adding a lot more than just 2 dicts, I shoulda caught that - sorry. in reality I have a for loop that generates dictionaries for each new number the loop finds in another list. the loop generates all the dicts I need, I just cant get them out of the loop! I figure I'll just make a list of dictionaries.
    – thejunior
    Jun 26, 2015 at 20:24
  • @thejunior you can do l = [] and inside you for loop do l.append(new_dict). Outside the for loop do BinTuple = tuple(l) Jun 26, 2015 at 20:26
  • @omerbp - I just did what you said and it worked PERFECTLY! As in my question, using .append did not work. I have no idea why .append didnt work before, but, like they taught me in the Navy - dont get stuck on why it works - think "FM" - "freakin' magic" - and keep going.
    – thejunior
    Jun 26, 2015 at 20:31

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.