Python Forum
Appending inside elements of lists, in an exact location
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Appending inside elements of lists, in an exact location
#1
In Python, how would I append to an element inside a list in a particular location? Ex.) if I have the list ['*4','*3','*2','*1'] and I want to add the elements [a,b, c] in a cycle, in between. So the result would be ['*a4', '*b3', '*c2', '*a1'].
Reply
#2
Given that you have two lists, you can use zip to iterate over them in parallel, and then you can concatenate in a loop. e.g.
first = "123"
second = "abc"

for num, letter in zip(first, second):
    print(num + letter)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question in this code, I input Key_word, it can not find although all data was exact Help me! duchien04x4 3 1,031 Aug-31-2023, 05:36 PM
Last Post: deanhystad
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 5,115 May-17-2022, 11:38 AM
Last Post: Larz60+
  Matching Exact String(s) Extra 4 1,906 Jan-12-2022, 04:06 PM
Last Post: Extra
  replace exact word marfer 1 6,144 Oct-11-2021, 07:08 PM
Last Post: snippsat
  List of lists - merge sublists with common elements medatib531 1 3,394 May-09-2021, 07:49 AM
Last Post: Gribouillis
  Assistance with running a few lines of code at an EXACT time nethatar 5 3,241 Feb-24-2021, 10:43 PM
Last Post: nilamo
  Sorting Elements via parameters pointing to those elements. rpalmer 3 2,585 Feb-10-2021, 04:53 PM
Last Post: rpalmer
Question How to print multiple elements from multiple lists in a FOR loop? Gilush 6 2,927 Dec-02-2020, 07:50 AM
Last Post: Gilush
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,367 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  Exact Match Kristenl2784 2 2,817 Jul-26-2020, 03:29 PM
Last Post: Kristenl2784

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020