Home > front end >  Printing a 2D-array
Printing a 2D-array

Time:09-26

So I am creating an exe file for a school project using python. In this program I am using 2D-arrays for the items in the catalog, so I can have their prices together. When I print the 2D-array the [] and the "" are also showing, how can I print them without showing?

This is my code:

coffee_m = ["Espresso", 1.80,], [ "Cappuccino", 1.80], ["Greek", 1.50]

This is a very simple version of the lists I have in my actual code

CodePudding user response:

You might use for loop with unpacking

coffee_m = ["Espresso", 1.80,], [ "Cappuccino", 1.80], ["Greek", 1.50]
for name, price in coffee_m:
    print(name, price)

output

Espresso 1.8
Cappuccino 1.8
Greek 1.5

observe that name and price are ,-sheared and can be accessed like in for loop with single variable in flat list.

CodePudding user response:

A simple solution can be

coffee_m = ["Espresso", 1.80,], [ "Cappuccino", 1.80], ["Greek", 1.50]

for coffee in coffee_m:
   print(*coffee)

* simply unpacks a list and return every element from it. If you want a seperator you can use

print(*coffee, sep=", ")
  • Related