0

I have two arrays, and one of them is an array containing arrays.

wordsEn = ["why", "because", "go"] 
wordsDe = [["warum"], ["weil", "da"], ["los", "gehen"]]

my code is

step = 0
size = 3
while step < size:
    word = input("Enter name of word: ")
    print("your word was " + word)
    if word in wordsEn:
       pos = wordsEn.index(word)
       print(wordsDe[pos])
       step = step + 1
    else:
        print("word not found.")

if i ask it print wordsDe[1] it prints ['weil', 'da']

How do I make it print it as a list like

weil da

SUmof1
  • 11

6 Answers6

0

wordsDe is a list and the 3 objects in the list are ["warum"], ["weil", "da"], and ["los", "gehen"].

Calling wordsDe[1] will give you the second object in the list ... ["weil", "da"]

If you want to print it

weil
da

you must call wordsDe[1][0] and then wordsDe[1][1]

0
step = 0
size = 3
while step < size:
    word = input("Enter name of word: ")
    print("your word was " + word)
    if word in wordsEn:
       pos = wordsEn.index(word)
       for i in range(len(wordsDe[pos])):
          print(wordsDe[pos][i])
       step = step + 1
    else:
       print("word not found.")

Enter name of word: because
your word was because
weil
da
Enter name of word: go
your word was go
los
gehen
Enter name of word: why
your word was why
warum

For array of arrays we should use two dimensions to access individual elements.

abhishek
  • 68
  • 7
0

You can use nested loops for this. Use two loops and a separator argument in print function. The separator is not necessary here

step = 0
size = 3
while step < size:

    word = input("Enter name of word: ")
    print("your word was " + word)
    if word in wordsEn:
        pos = wordsEn.index(word)

        for item in wordsDe[pos]:
            print(item,sep="\n")

        step = step + 1
    else:
        print("word not found.")
  • I think you meant the `end` argument rather than `sep`, which is the default anyway and is not necessary. `sep` is only relevant when you pass multiple arguments – Tomerikoo May 17 '20 at 08:08
0

You can cycle wordsDe[pos] and simply print contents one by one.

step = 0
size = 3
while step < size:
    word = input("Enter name of word: ")
    print("your word was " + word)
    if word in wordsEn:
       pos = wordsEn.index(word)
       for data in wordsDe[pos]:
           print (data)
       step = step + 1
    else:
        print("word not found.")
LDMdev
  • 1,280
  • 2
  • 10
  • 13
0

As your list is nested, means lists containing lists. You have to specify the output format for such lists according to your desired output.

For given situation you can use something list this:

print(*wordsDe[1], sep='\n') # First unpacks all items in the list using '*', 
                             # then tells 'print()' to separate each item using '\n'
Zain Arshad
  • 1,885
  • 1
  • 11
  • 26
-2

Change list's items to string:

wordsEn = ["why", "because", "go"] 
wordsDe = [["warum"], ["weil", "da"], ["los", "gehen"]]

step = 0 ;size = 3 
while step < size: 
    word = input("Enter name of word: ") 
    print("your word was " + word) 
    if word in wordsEn: 
        pos = wordsEn.index(word) 
        w=''
        for wd in wordsDe[pos]:
            w+=wd+' '
        print(w)
        step = step + 1 
    else: 
        print("word not found.")
Alchimie
  • 426
  • 4
  • 12
  • That prints the list in one line - not what is asked for. And would anyway be achieved easier by a simple `print(*wordsDe[ps])` – Tomerikoo May 17 '20 at 08:12