nana
nana

Reputation: 59

How to get all the elements in two lists to return a string

I have two lists of strings. Both lists can be longer, but will always be the same length. I want to return a string.

list1 = ['a', 'b', 'c']
list2 = ['3', '2', '1']

I made a function hug so that hug('a', '3') gets me:

'333a333'

The numbers indicate how many there will be.

With the lists, I want to get:

'333a33322b221c1'

I currently have:

list3 = [[x, y] for x, y in zip(list1, list2)]

Which gives me

[['a', '3'], ['b', '2'], ['c', '1']]`

I'm not sure where to go from here. I want to take each new list I made and put them all in hug(x, y) but I keep getting errors.

EDIT: Thank you for all the solutions, but I'm fine with what I have right now. I already figured out the hug part. What I was trying to do now, is use hug() to get the string. I know

hug('a', '3') + hug('b', '2') + hug('c', '1')

But I don't want to have to type that all out, especially if the lists are longer.

list1 = ['a', 'b', 'c', 'd', 't']
list2 = ['3', '2', '1', '5', '4']

Upvotes: 1

Views: 554

Answers (3)

aneroid
aneroid

Reputation: 15962

To continue from what you already have:

I made a function hug so that hug('a', '3') gets me:

'333a333'

You haven't shown your code for hug but this would be your next step:

  1. Use zip() to get each pair of items from the two lists
  2. call hug() with each pair, as a list comprehension or generator expression
    • since we don't need a list of the pairs, only need to iterate over them
  3. use str.join() to join each item in the list/generator with the empty string '' (nothing) between them.
''.join(hug(letter, number) for letter, number in zip(list1, list2))

# output:
'333a33322b221c1'

Note that list3 in your could have been created more easily as:

list3 = list(zip(list1, list2))

# but since we're iterating over it just once, just keep it as a zip object:
list3 = zip(list1, list2)

# or directly use the zip expression in the for loop, as above

Upvotes: 0

Mad Physicist
Mad Physicist

Reputation: 114230

Putting [x, y] in your comprehension is almost a no-op, since zip is already giving you a tuple.

Instead, you can apply hug to every item in the zip:

[hug(x, y) for x, y in zip(list1, list2)]

You can compact this using argument unpacking notation:

[hug(*e) for e in zip(list1, list2)]

The next step is to join all the resulting steps into a string:

''.join([hug(*e) for e in zip(list1, list2)])

You don't need to make an entire list to do this. Since hug always returns a string, you can pass a generator to join:

''.join(hug(*e) for e in zip(list1, list2))

Upvotes: 1

Yam Mesicka
Yam Mesicka

Reputation: 6581

That's a good start. Now you need to:

  1. Create a short "hugged" strings ('333a333', '22b22' etc.)
  2. Concatenate all the results.

To achieve 1, you can use Python's * operator. 's' * n concatenate the s string to itself n times:

def create_hugged(s: str, number: int) -> str:
    hug_side = str(number) * number
    return hug_side + s + hug_side

To achieve 2, use your list of lists to create a single list of hugged numbers:

hugged = [create_hugged(s, number) for s, number in list3]

Now you have list of "hugged" strings:

hugged == ['333a333', '22b22', '1c1']

So let's just join them using str.join:

final_result = ''.join(hugged)

Upvotes: 1

Related Questions