Changing elements of list to string - python

I have a list [['user44'],['user204'],['user355'],['user395'],['user452']]
I want to convert it to [ 'user44', 'user204' , 'user355', 'user395', 'user452']
Any help on this?

Assuming userXYZ is always a string
lst = [['user44'],['user204'],['user355'],['user395'],['user452']]
lst1 = [y for x in lst for y in x]
print(lst1)
output
['user44', 'user204', 'user355', 'user395', 'user452']

Every element of the list is a list itself, with one element.
You can take that element and create a new list using list comprehension.
new_list=[elem[0] for elem in old_list]
This will go over all elements of the list (which are themselves, lists), and extract the strings into a new list.

This will allow for use encase you need to join more than 1 list.
x = [''.join(x[i] for i in range(len(x)))]

Related

How to convert a list made up of one tuple into a two item list?

I have a function that returns a one item list, like so:
list = [('array_1','array_2')]
I want to change this so that the list is instead a two item one, without the parentheses or single quotes:
list = [array_1,array_2]
What would be the best way to go about doing this?
Try this
lists = [('array_1','array_2')]
print([y for x in lists for y in x])
output
['array_1', 'array_2']
Use chain.from_iterable
from itertools import chain
list(chain.from_iterable([('array_1','array_2')]))
['array_1', 'array_2']
You can try this:
lst_tuple = [('array_1', 'array_2')]
lst = []
for i in lst_tuple[0]:
lst.append(i)
By iteratating over the list that contains the tuple and appending each item to a new list, you can get this result:
['array_1', 'array_2']
You could just typecast like this.
list = list([('array_1','array_2')][0])

How to make a list to split into each element and make a sublist

If I have this list,
list01 = ['GAGGT','TCCGT','ABECF']
I want to make each element in the list to split, but still remain in the same box of list.
Like this:
listalt = [['G','A','G','G','T'],['T','C','C','G','T'],['A','B','E','C','F']]
listalt = [list(i) for i in list01]
This is a list comprehension, and uses the fact that list can take an iterable (like a string) and turn it into a list
You can use this :
final_list=[]
for item in list01:
final_list.append(list(item))
print(final_list)
which is same as :
print([list(item) for item in list01])

Remove item from a list element?

This may seem odd, but I am trying to remove a part of an item contained in a list. Basically, I am trying to remove a specific character from multiple list elements. For example
list = ['c1','c2','c3','d1','s1']
list.remove('c')
I know that doing that wouldn't work, but is there any way to remove the "c"s in the list, and only the "c"s in Python 3?
lst = [s.replace('c','') for s in lst]
# ['1','2','3','d1','s1']
List comprehensions are your friend. Also note the "list" is a keyword in Python, so I highly recommend you do not use it as a variable name.
Use list comprehensions,
list = ['c1','c2','c3','d1','s1']
list_ = [ x for x in list if "c" not in x ] # removes elements which has "c"
print list_ # ['d1', 's1']
list1 = ['c1','c2','c3','d1','d2']
list2 = []
for i in range (len(list1)):
if 'c' not in list1[i]:
list2.append(list1[i])
print (list2) #['d1', 'd2']
and also this link may helpful
Link one

Put average of nested list values into new list

I have the following list:
x = [(27.3703703703704, 2.5679012345679, 5.67901234567901,
6.97530864197531, 1.90123456790123, 0.740740740740741,
0.440136054421769, 0.867718446601942),
(25.2608695652174, 1.73913043478261, 6.07246376811594,
7.3768115942029, 1.57971014492754, 0.710144927536232,
0.4875, 0.710227272727273)]
I'm looking for a way to get the average of each of the lists nested within the main list, and create a new list of the averages. So in the case of the above list, the output would be something like:
[[26.315],[2.145],[5.87],etc...]
I would like to apply this formula regardless of the amount of lists nested within the main list.
I assume your list of tuples of one-element lists is looking for the sum of each unpacked element inside the tuple, and a list of those options. If that's not what you're looking for, this won't work.
result = [sum([sublst[0] for sublst in tup])/len(tup) for tup in x]
EDIT to match changed question
result = [sum(tup)/len(tup) for tup in x]
EDIT to match your even-further changed question
result = [[sum(tup)/len(tup)] for tup in x]
An easy way to acheive this is:
means = [] # Defines a new empty list
for sublist in x: # iterates over the tuples in your list
means.append([sum(sublist)/len(sublist)]) # Put the mean of the sublist in the means list
This will work no matter how many sublists are in your list.
I would advise you read a bit on list comprehensions:
https://docs.python.org/2/tutorial/datastructures.html
It looks like you're looking for the zip function:
[sum(l)/len(l) for l in zip(*x)]
zip combines a collection of tuples or lists pairwise, which looks like what you want for your averages. then you just use sum()/len() to compute the average of each pair.
*x notation means pass the list as though it were individual arguments, i.e. as if you called: zip(x[0], x[1], ..., x[len(x)-1])
r = [[sum(i)/len(i)] for i in x]

How can i separate a string that is in a list into 2 parts?

I currently have a list that looks like the following:
list = ['325 153\n', '509 387\n', '419 397\n']
I am wondering how could i access these strings individually so i could use it in a function such as
for (x, y) in list:
where x is the first number of the string and y is the second number (separated by the space)
Is a good way to do this to turn the string into tuples somehow? I have tried using the 'split' function as i believe it is not valid for a list.
>>> a_list = ['325 153\n', '509 387\n', '419 397\n']
>>> [ i.split() for i in a_list ]
[['325', '153'], ['509', '387'], ['419', '397']]
You can iterate one the elements of the list:
for elem in list:
a, b = elem.split()[0], elem.split()[1]
a and b will be the elements and you can process them just as you wish.
[tuple(x.split()) for x in list]
this will return you a list of tuples
There is the split() method for string. Before using it you should use strip() to get rid of the training \n. At the end you can put both into a list comprehension:
for x, y in [item.strip().split(" ", 1) for item in lst]:
Note that I replace list with lst. list is a built-in type and should not be used as variable name.

Categories

Resources