Display image based on user selection

Hello, I want to display the image based on the item the user selects. Right now, I’m only able to do it manually by using x amount of else if statements. Ideally, I do not want to do that if I have like hundreds of items.

I tried to use loops but I didn’t manage to get it to work. Please assist, thank you.

sample dataset:

Image_Url Item_Name
https://m.media-amazon.com/images/I/41sHexVm4NL.AC_US60_SCLZZZZZZZ_.jpg Google Pixel 5A
https://m.media-amazon.com/images/I/41YNaNTqJoL.AC_US60_SCLZZZZZZZ_.jpg Google Pixel 6
https://m.media-amazon.com/images/I/41t1HM8UN8L.AC_US60_SCLZZZZZZZ_.jpg Moto G Power
https://m.media-amazon.com/images/I/41UuyU7HsPL.AC_US60_SCLZZZZZZZ_.jpg Samsung Galaxy S20
https://m.media-amazon.com/images/I/31XThr46I6L.AC_US60_SCLZZZZZZZ_.jpg SAMSUNG SM-A125FZKHXSP Galaxy A12

Below are the codes:

import streamlit as st
import pandas as pd

df = pd.read_csv(‘phones.csv’)
image_list = df.iloc[:, 0].unique().tolist()
item_list = df.iloc[:, 1].unique().tolist()

user_option = st.selectbox(‘Choose an item’, [’–Select–’] + item_list)
if user_option == ‘–Select–’:
st.info(‘You can view the image here’)
elif user_option == item_list[0]:
st.image(image_list[0])
elif user_option == item_list[1]:
st.image(image_list[1])
elif user_option == item_list[2]:
st.image(image_list[2])
elif user_option == item_list[3]:
st.image(image_list[3])
elif user_option == item_list[4]:
st.image(image_list[4])

You can make a dict from the lists:

images = dict(zip(item_list, image_list))

user_option = st.selectbox(‘Choose an item’, [’–Select–’] + item_list)

st.image(images[user_option])

Best,
Randy

@randyzwitch Thank you!