Click here to Skip to main content
15,912,578 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Write a python program to define a tuple to accept 3 food product details such as Products name with their Price and Expiry date in a sub tuple then change the order of price and expiry date in each tuple element

the output should be like this
tuple=(('Cake'(500,'12-09-2020')),('Jelly'(10,'10-09-2020')),('Cream'(256,'12-11-2020'))
tuple=(('Cake'('12-09-2020',500,)),('Jelly'('10-09-2020',10)),('Cream'('12-11-2020',256)))

What I have tried:

t=(('CAKE', (748.0, '07-09-2020')), ('JELLY', (12.0, '07-09-2020')), ('CREAM', (244.0, '03-11-2020')))
for i in range(0,len(t)):
a=t[i][1][0];b=t[i][1][1]
a,b=b,a
print(t)
Posted
Updated 2-Sep-20 23:46pm

1 solution

Try this:
Python
newlist=()
list=(('CAKE', (748.0, '07-09-2020')), ('JELLY', (12.0, '07-09-2020')), ('CREAM', (244.0, '03-11-2020')))
for element in list:
    newitem = (element[0], (element[1][1], element[1][0]))
    newlist = newlist + newitem
print(newlist)
 
Share this answer
 
Comments
Member 14928977 3-Sep-20 5:47am    
IT WORKED, THANK YOU SO MUCH
Richard MacCutchan 3-Sep-20 5:51am    
You are welcome. But I recommend that you go to 5. Data Structures — Python 3.7.9 documentation[^] and study the different collection types in Python.
CPallini 3-Sep-20 5:51am    
5.
Richard MacCutchan 3-Sep-20 5:51am    
Thanks Carlo.
Maciej Los 3-Sep-20 6:08am    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900