'python import a list from another file to pick random

When I copy and paste the list to the file, it successfully picks random but not from the words.py file. I must have something to do with concatenation type mismatch because it gives me an error

print('random word is ' + random.choice(example))
TypeError: can only concatenate str (not "dict") to str

This is the code I am using.

import random
from words import words 
import string

print('random word is ' + random.choice(words))

This is the beginning portion of the dictionary. It has over 5000 words.

words = [{"data":["aback","abaft","abandoned","abashed","aberrant","abhorrent","abiding","abject","ablaze","able","abnormal","aboard","aboriginal","abortive","abounding","abrasive","abrupt","absent","absorbed","absorbing","abstracted","absurd","abundant","abusive","accept","acceptable","accessible","accidental","account","accurate","achiever","acid","acidic","acoustic","acoustics","acrid","act"]}]



Solution 1:[1]

You are passing a dict to random.choice. You need to pass the list of "data" instead:

print('random word is ' + random.choice(words[0]["data"]))

A breakdown of what's happening

# Tiny example of words
words = [{"data": ["aback"]}]
>>> words[0]
{"data": ["aback"]}
>>> words[0]["data"]
["aback"]
>>> words[0]["data"][0]
"aback"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Freddy Mcloughlan