Python Forum
dict value, how to change type from int to list?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
dict value, how to change type from int to list?
#1
Question 
I have a dictionary, and one of the keys currently has 1 as value, so the value side is an int. I'm trying to make the value side a list so it can have [1, 2].

I currently have: {'key1': 1}
I'm trying to get: {'key1': [1, 2]}

test = dict()
test['key1'] = 1

# i tried without success:
# this will do an addition
test['key1'] += 2
# this will replace the current value
test.update({'key1': 2})
thank you!
Reply
#2
If it's just this one case, just replace it.

test['key1'] = [test['key1'], 2]
The problem is if you need to do this and might want to keep extending the list. Best in that case is to never insert the int in the first place. Always have a list as the value (perhaps via defaultdict). Then you can just append whenever necessary.

import defaultdict
test = defaultdict(list)

#first element
test['key1'].append(1)
# second element
test['key1'].append(2)
buran likes this post
Reply
#3
test = {'key':[1] }  # Initialize with a list value
test['key'] += [2]  # Then you can add to the list like this
test['key'].append(3)  # or this
test['key'].extend((4, 5, 6))  # or this
print(test['key'])
Output:
[1, 2, 3, 4, 5, 6]
Reply
#4
One way is to use setdefault.

>>> test = dict()
>>> test.setdefault('key1', list()).append(2)
>>> test
{'key1': [2]}
It could be used in for-loops etc:

>>> test = dict()
>>> nums = [2, 3, 7, 19, 24]
>>> for num in nums:
...     if num % 2:
...         test.setdefault(num, list()).append(num**2)
...
>>> test
{3: [9], 7: [49], 19: [361]}
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Change font in a list or tuple apffal 3 396 Oct-14-2021, 05:26 AM
Last Post: ndc85430
  Change a list to integer so I can use IF statement buckssg 3 463 Sep-21-2021, 02:58 AM
Last Post: bowlofred
  change csv file into adjency list ainisyarifaah 0 289 Sep-21-2021, 02:49 AM
Last Post: ainisyarifaah
  How to change odd to even numbers in the list? plumberpy 8 798 Aug-08-2021, 11:07 AM
Last Post: plumberpy
  You have any idea, how fix TypeError: unhashable type: 'list' lsepolis123 2 1,058 Jun-02-2021, 07:55 AM
Last Post: supuflounder
  Question about change hex string to integer sting in the list (python 2.7) lzfneu 1 809 May-24-2021, 08:48 AM
Last Post: bowlofred
  type change of dbshelve key between Python 2 and 3 ccaudle 3 904 Feb-09-2021, 05:13 PM
Last Post: ccaudle
  What type of *data* is the name of a list/tuple/dict, etc? alloydog 9 1,505 Jan-30-2021, 07:11 AM
Last Post: alloydog
  LIST or ARRAY Comparison and change of value nio74maz 0 669 Dec-21-2020, 05:52 PM
Last Post: nio74maz
  How to remove dict from a list? Denial 7 1,158 Sep-28-2020, 02:40 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020