tegge-classroom / STAT2984-2018

STAT 2984: Statistical Programming I - Spring 2018
1 stars 13 forks source link

Dictionary- Type differences #14

Open tfinch21 opened 6 years ago

tfinch21 commented 6 years ago

Do you have to do anything different for an int value versus a string in a dictionary?

matter698no2 commented 6 years ago

You shouldn't have issues with data types if you contain your dictionary values in lists.

dictionary = { 'things': []}

So now if you want to add values to a key, it's as easy as:

#adding a string
dictionary['things'].append('number of problems')

#adding an interger
dictionary['things'].append(99)

#the dictionary now looks like this:
{'things: ['number of problems', 99]}

If you wanted two different keys in the dictionary, one with int values and one with string values, you'd just make another key in the dictionary

dictionary2 = {'numbers': [], 'not numbers':[]}

#adding a string to not numbers
dictionary['not numbers'].append('not a number')

#adding a number to numbers
dictionary['numbers'].append(1)

#dictionary looks like this
{'numbers': [1], 'not numbers': ['not a number']}