Susu
Susu

Reputation: 43

Make a word for every lists of list

I want to convert every lists of list to a string.

myList=[["aaaa"],["bbb"]]

I want to have

aaaa

bbb

I tried

for i in range(len(myList)):
    url2=("".join(myList[i]))
    print(url2)

Upvotes: 0

Views: 28

Answers (1)

Gino Mempin
Gino Mempin

Reputation: 29674

First, you don't need to do range(len(myList)). You can just do for <element> in <list> to loop through the list.

myList=[["aaaa"],["bbb"]]

for subList in myList:
    print(subList)

Output:

['aaaa']
['bbb']

Next, once you have access to each sub-list, you just have to convert its elements to strings. From your example, the elements are already strings (wrapped in ' '), so you don't need to use join. Just access the [0]-th element.

myList=[["aaaa"],["bbb"]]

for subList in myList:
    print(subList[0])

Output:

aaaa
bbb

However, if every sub-list contains more than 1 element, then you can use join:

myList=[
    ["aaaa", "AAAA", "aaaa"],
    ["bbb", "BBBB", "bbbb"]
]

for subList in myList:
    combined = "".join(subList)
    print(combined)

Output:

aaaaAAAAaaaa
bbbBBBBbbbb

Upvotes: 3

Related Questions