# Dropdown options, pass a variable

**URL:** <https://community.plotly.com/t/dropdown-options-pass-a-variable/45676>\
**Category:** Dash Python\
**Created:** [October 4, 2020, 9:27pm UTC](https://community.plotly.com/t/dropdown-options-pass-a-variable/45676 "2020-10-04T21:27:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![thibaultmagy](https://avatars.discourse-cdn.com/v4/letter/t/db5fbb/32.png) [@thibaultmagy](https://community.plotly.com/u/thibaultmagy)\
**Post date:** [October 4, 2020, 9:27pm UTC](https://community.plotly.com/t/dropdown-options-pass-a-variable/45676/1 "2020-10-04T21:27:58Z")

</div>

Hi, I am try to pass a variable into options dropdown attribute so I can load any variable I want in it.  
Let’s say I want to use a file to create a list of options like this :

```auto
def get_countries():
    """Load all countries from a file"""
    countries = []
    with open('./data/countries.txt', 'r', encoding='utf-8') as countries_file:
        for country in countries_file:
            country_data = country.split('|')
            countries.append(
                {
                    'label': country_data[0],
                    'value': country_data[1].strip('\n')
                }
            )
    return countries

```

Assuming the file look like somthing like this :

```auto
AF|Afghanistan
AL|Albania
DZ|Algeria
...

```

I put the return value of my function _get\_countries_ in a variable _countries_ :

But when I try to put it in the dropdown attribute :

```auto
dcc.Dropdown(
            id='country',
            options=[countries],
            value='AF'
        ),

```

I get the error :

Invalid argument `options[0]` passed into Dropdown with ID “country”.  
Expected `object`.  
Was supplied type `array`.

I would like to know if it is possible, and if it is : how to proceed. I tried to get the object Options from html.Option(), but it doesn’t work.

Thank you for your help.

---

<div class="post-metadata">

**Author:** ![AnnMarieW](https://avatars.discourse-cdn.com/v4/letter/a/ec9cab/32.png) [@AnnMarieW](https://community.plotly.com/u/AnnMarieW)\
**Post date:** [October 4, 2020, 10:49pm UTC](https://community.plotly.com/t/dropdown-options-pass-a-variable/45676/2 "2020-10-04T22:49:41Z")

</div>

Hi @thibaultmagy and welcome to the Dash community!

Assuming that your function works correctly and if you print(countries) right before the return, you get something that looks like:  
[{‘label’: ‘AF’, ‘value’: ‘Afghanistan’}, {‘label’:‘AL’, ‘value’: ‘Albania’}, … etc]

Instead of: `options=[countries],`

Try: `options=get_countries(),`

---

<div class="post-metadata">

**Author:** ![thibaultmagy](https://avatars.discourse-cdn.com/v4/letter/t/db5fbb/32.png) [@thibaultmagy](https://community.plotly.com/u/thibaultmagy)\
**Post date:** [October 7, 2020, 4:23pm UTC](https://community.plotly.com/t/dropdown-options-pass-a-variable/45676/3 "2020-10-07T16:23:27Z")

</div>

Hi thank you for your answer, it works.

The return looked exactly like this.

Thank you again.
