-1

For example I have 2 lists:

a = ['st', 'nd', 'rd', 'th']
b = [1, 2, 3, 4, 5, 6, 7, 8]

I want the combined output of both lists, I want it to print it like this:

1st
2nd
3rd
4th
5th
6th
7th
8th

My a list has only 4 items but I want the last item to repeat until the loop is complete.

This is the code I have tried so far:

for i in b:
  print(str(i) + b)

But as we all know this gives the whole list of b.

  • 1
    [Is there a zip-like function that pads to longest length?](https://stackoverflow.com/a/1277311) might help. – 001 Jun 25 '21 at 14:26
  • 1
    I personally did not downvote this question but if you hover over the downvote button you'll see "This question does not show any research effort". You don't appear to have attempted to solve this problem and you haven't provided any code that we can help with. Those are common causes for downvotes. – jarmod Jun 25 '21 at 14:27
  • 1
    `for _ in [ch for ch in a] + [a[-1] for i in range(len(b)-len(a))]: print(_)` A quick algo, probably a faster one that already exists but i'm unaware of it. – TheLazyScripter Jun 25 '21 at 14:31
  • For a more general solution (because the proposed solutions will all fail for 21, yielding 21th), see https://stackoverflow.com/questions/9647202/ordinal-numbers-replacement – jarmod Jun 25 '21 at 14:44

5 Answers5

2

You can use itertools.zip_longest passing the default value as the fillvalue keyword argument.

>>> from itertools import zip_longest
>>> [''.join((str(s) for s in pair)) for pair in zip_longest(b,a, fillvalue=a[-1])]

['1st', '2nd', '3rd', '4th', '5th', '6th', '7th', '8th']
ThePyGuy
  • 17,779
  • 5
  • 18
  • 45
1
def concatenate_lists(shorter, longer):
  output_list = []
  for i, long_item in enumerate(longer):
    prefix = str(long_item)
    if i < len(shorter):
      suffix = str(shorter[i])
    else:
      suffix = str(shorter[-1])
    output_list.append(prefix + suffix)
  return output_list
    

concatenate_lists(a, b)
1

My suggestion:

res=[str(i)+k for (i, k) in zip(b, a+[a[-1]]*(len(b)-len(a)))]

>>>print(res)

['1st', '2nd', '3rd', '4th', '5th', '6th', '7th', '8th']
IoaTzimas
  • 10,538
  • 2
  • 13
  • 30
1
a = ['st', 'nd', 'rd', 'th']
b = [1, 2, 3, 4, 5, 6, 7, 8]
[str(el) + a[b.index(el)] if b.index(el) < len(a)-1  else str(el) + a[-1] for el in b]

['1st', '2nd', '3rd', '4th', '5th', '6th', '7th', '8th']

labzus
  • 137
  • 1
  • 5
0

You need the min built-in method

suffixes = ["st", "nd", "rd", "th"]
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

suffix_max_idx = len(suffixes) - 1    # Pre-calculate this to make it faster

combined = [];    # Holder for the finished product

# Use enumerate because we're going to need the index value
for num_idx, num in enumerate(numbers):
    # Get the suffix index, until the `num_idx` is greater than `suffix_max_idx`
    # it will match along with the `num_idx` value.
    suffix_idx = min([num_idx, suffixes_max_idx])
    suffix = suffixes[suffix_idx]
    combined.append(f'{str(num)}{suffix})
    

Same thing more concise with list comprehension

suffixes = ["st", "nd", "rd", "th"]
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

combined = [
    f'{n}{suffixes[min([i, len(suffixes) - 1])]}' for i, n in enumerate(numbers)
]
print(combined)
# ['1st', '2nd', '3rd', '4th', '5th', '6th', '7th', '8th', '9th', '10th']
Rowshi
  • 360
  • 2
  • 11