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

thejunior
  • 15
  • 4

1 Answers1

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)
AvidLearner
  • 4,123
  • 5
  • 35
  • 48
  • what's the difference with binList = (bin1, bin2) ? – Ed_ Jun 26 '15 at 20:02
  • @WeaponX Tuples are immutable in python - read more [here](http://stackoverflow.com/questions/1538663/why-are-python-strings-and-tuples-are-made-immutable) and [here] (http://www.tutorialspoint.com/python/python_tuples.htm) – AvidLearner Jun 26 '15 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 '15 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)` – AvidLearner Jun 26 '15 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 '15 at 20:31
  • @thejunior haha I'm glad that helped you... the reason why it didn't work for you - `append` is not possible with a dict (as `bin1`), but it **is** possible with a list. in the example I gave here, `l` is a list – AvidLearner Jun 26 '15 at 20:33
  • @thejunior No problem... consider accepting :) this way, people will know it solves the question (and you get +2 points) – AvidLearner Jun 29 '15 at 10:39