newcool
newcool

Reputation: 319

Python How to insert characters in every other listed item

list = ['username1', 'https://link.com/bsd3nj', 'username2', 'https://link.com/a32fs2', 'username3', 'https://link.com/cfzxso']

sort_list = '* {}'.format('- \n'.join(list))

Output:

* username1- 
https://link.com/bsd3nj-
username2- 
https://link.com/a32fs2-
username3- 
https://link.com/cfzxso-
username4- 
https://link.com/a235jsh-
username5- 
https://link.com/123nls2-

This is what I have, but its not what I want happen.

I want the list to be like

* username1 - https://link.com/bsd3nj
* username2 - https://link.com/a32fs2
* username3 - https://link.com/cfzxso
* username4 - https://link.com/a235jsh
* username5 - https://link.com/123nls2

I have thought about doing .split() but there isnt really any pattern. The username is random so it the link ending.

Upvotes: 0

Views: 65

Answers (4)

LeoE
LeoE

Reputation: 2083

Here as a one-liner:

l = ['username1', 'https://link.com/bsd3nj', 'username2', 'https://link.com/a32fs2', 'username3', 'https://link.com/cfzxso']

out = '* {}'.format('\n* '.join(list(map(" - ".join, zip(l[::2], l[1::2])))))

Upvotes: 1

Bill Chen
Bill Chen

Reputation: 1749

I like to use numpy and pandas.

import numpy as np 
import pandas as pd 

l = ['username1', 'https://link.com/bsd3nj', 'username2', 'https://link.com/a32fs2', 'username3', 'https://link.com/cfzxso']

l = np.array(l)  # convert to numpy array so I can reshape it into two column
l = l.reshape(-1,2)
l = pd.DataFrame(l) # convert to data frame so I can easily use operator.

l = "*  " + l[0] +  " - " + l[1]
l = l.values.tolist()  # convert back to list
l = "\n".join(l) # convert to string with line break.
print(l)

Output is:

*  username1 - https://link.com/bsd3nj
*  username2 - https://link.com/a32fs2
*  username3 - https://link.com/cfzxso

Upvotes: 0

azro
azro

Reputation: 54148

You shouldn't use a built-in name for a variable : list, it'll save you from errors a day if you don't think about it


That said, to make pairs from consecutive elements there is several options (listed here)

Here's one : zip(values[::2], values[1::2])

for name, link in zip(values[::2], values[1::2]):
    print(f"*{name} - {link}")

# or shorter

sort_list = "\n".join([f"*{name} - {link}" for name, link in zip(values[::2], values[1::2])])
print(sort_list, "\n\n")

The slice notation [start:end:increment] is here

  • [::2] : all values from start, 2 by 2 => all the names
  • [1::2] : all values except first one, 2 by 2 => all the links

The zip operation make an iterator that aggregates elements from each of the iterables.

zip([1,2,3], ['a', 'b', 'c']) # => ((1,'a'), (2, 'b'), ('3, 'c'))

So you have a list of names and a list of links, and you pair them

print(values[::2])  # ['username1', 'username2', 'username3']
print(values[1::2])  # ['https://link.com/bsd3nj', 'https://link.com/a32fs2', 'https://link.com/cfzxso']
print(list(zip(values[::2], values[1::2]))) # [('username1', 'https://link.com/bsd3nj'), ('username2', 'https://link.com/a32fs2'), ('username3', 'https://link.com/cfzxso')]

Upvotes: 2

jimsu2012
jimsu2012

Reputation: 170

Using an iterator is an efficient solution:

def joined(given_list):
    iterator = iter(given_list)
    return '\n'.join(['* ' + c + ' - ' + next(iterator) for c in iterator])

Upvotes: 1

Related Questions