Python3 add multiple line of strings into one list - python-3.x

I have strings in multiple lines, but my requirement is to have those strings in one list
['arn:aws:iam::****:saml/Prod']
['arn:aws:iam::***:saml/Test']
['arn:aws:iam::*****:saml/Dev']
Into one list, My expected output is
['arn:aws:iam::****:saml/Prod', 'arn:aws:iam::***:saml/Test', 'arn:aws:iam::*****:saml/Dev']
into one list.
How can I do this? appreciate your help

Assuming "my_list" really is just a list of lists that each include a single string, your easiest answer is a list comprehension:
In [14]: my_list = [
...: ['arn:aws:iam::****:saml/Prod'],
...: ['arn:aws:iam::***:saml/Test'],
...: ['arn:aws:iam::*****:saml/Dev']
...: ]
In [15]: new_list = [item[0] for item in my_list]
In [16]: new_list
Out[16]:
['arn:aws:iam::****:saml/Prod',
'arn:aws:iam::***:saml/Test',
'arn:aws:iam::*****:saml/Dev']
But, to echo the first comment, there could potentially be more context here that might require more dynamic handling, etc.

Related

List of List- delete characters

I have a huge list like
list = [["a", "bfgf", "c%2"], ["b", "hhj", "kkkk", "f%2"]]
I want to remove %2 end of every last item in lists of list.
I tried
list = [[item[-1].replace('%2', '') for item in lst] for lst in list]
and it did not work
Your way does not work because you are not changing the value in place. Your statement simply takes a copy of every sub-list, stores it in lst, then changes the data in that copy, and not the actual list itself.
Try it in this way, and tell me if it works.
myList= [["a","bfgf","c%2"],["b","hhj","kkkk","f%2"]]
for i in range(len(myList)):
Last=myList[i].pop()
Last=Last.replace("%2","")
myList[i].append(Last)
print (myList)
For your nested list you can also use a nested list comprehension to remove the unwanted characters.
ini_list = [["a", "bfgf", "c%2"],["b", "hhj", "kkkk", "f%2"]]
ini_list = [[val.replace('%2', '') for val in sublist] for sublist in ini_list]
print(ini_list)
# output
[['a', 'bfgf', 'c'], ['b', 'hhj', 'kkkk', 'f']]

how to extract lists having same element value?

I have a list of list like this data=[["date1","a",14,15],["date1","b",14,15],["date1","c",14,15],["date2","a",14,15],["date2","b",14,15],["date2","c",14,15],["date3","a",14,15],["date3","b",14,15],["date3","c",14,15]] I want to get lists having the same 2nd index. i tried this code but i got 9 lists when i just need 3 lists.
data=[["date1","a",14,15],["date1","b",14,15],["date1","c",14,15],["date2","a",14,15],["date2","b",14,15],["date2","c",14,15],["date3","a",14,15],["date3","b",14,15],["date3","c",14,15]]
for i in data:
a=[]
for j in data:
if (i[1]==j[1]):
a.append(j)
print(a)
i expected to get ["date1","a",14,15],["date2","a",14,15],["date3","a",14,15]
["date1","b",14,15],["date2","b",14,15],["date3","b",14,15]
["date1","c",14,15],["date2","c",14,15],["date3","c",14,15]
data=[["date1","a",14,15],["date1","b",14,15],["date1","c",14,15],["date2","a",14,15],["date2","b",14,15],["date2","c",14,15],["date3","a",14,15],["date3","b",14,15],["date3","c",14,15]]
from itertools import groupby
from operator import itemgetter
print(
[list(v) for k,v in groupby(sorted(data, key=itemgetter(1)), key=itemgetter(1))]
)
In order for groupby to work the data has to be sorted.
Depending on your use case, the list instantiation of the iterator might not be needed. Added it to see proper output instead of <itertools._grouper... >

Printing a list method return None

I am an extremely begginer learning python to tackle some biology problems, and I came across lists and its various methods. Basically, when I am running print to my variable I get None as return.
Example, trying to print a sorted list assigned to a variable
list1=[1,3,4,2]
sorted=list1.sort()
print(sorted)
I receive None as return. Shouldn't this provide me with [1,2,3,4]
However, when printing the original list variable (list1), it gives me the sorted list fine.
Because the sort() method will always return None. What you should do is:
list1=[1,3,4,2]
list1.sort()
print(list1)
Or
list1=[1,3,4,2]
list2 = sorted(list1)
print(list2)
You can sort lists in two ways. Using list.sort() and this will sort list, or new_list = sorted(list) and this will return a sorted list new_list and list will not be modified.
So, you can do this:
list1=[1,3,4,2]
sorted=sorted(list1)
print(sorted)
Or you can so this:
list1=[1,3,4,2]
list1.sort()
print(list1)

Python: logarithm to base 2 in nested lists

I have this list:
mylist = [['A',5,6,7],['C',3,90,5.3],['G',5,4,6]]
How I can get the logarithm to base 2 in such nested lists?
This is my approach:
from math import log
print([log(x,2) for x in mylist2[1:]])
Outputs should be like:
[['A',2.321928094887362, 2.584962500721156, 2.807354922057604],['C',1.5849625007211563, 6.491853096329675, 2.4059923596758366],['G',2.321928094887362, 2.0, 2.584962500721156]]
Not exactly clear on how you want your output to be formatted. But if you want to keep it in the same "format" as the input, here's a quick one-liner:
In [6]: list(map(lambda row: [row[0]] + [log(item, 2) for item in row[1:]], mylist))
Out[6]:
[['A', 2.321928094887362, 2.584962500721156, 2.807354922057604],
['C', 1.5849625007211563, 6.491853096329675, 2.4059923596758366],
['G', 2.321928094887362, 2.0, 2.584962500721156]]
If you just want the log values without the "key" to start each list, leave off the [row[0]] + part.
HTH.
The fastest approach (not in computational terms obviously) is a naive solution like this:
from math import log
mylist = [['A',5,6,7],['C',3,90,5.3],['G',5,4,6]]
for numbers in mylist:
for number in numbers[1:]:
print log(number,2) #or assign to something

reverse numbers in a list of tuples

I have some coordinate data that is lng,lat however I need it lat,lng
I have them in a list of tuples and need to switch them around:
myList = [(-87.93897686650001, 41.8493707892),
(-87.93893322819997, 41.8471652588),
(-87.9292710931, 41.8474548975),
(-87.91960917239999, 41.8477438951),
(-87.91927828050001, 41.8404034535)]
I have tried
newList = []
for i in range(len(myList)):
for c, x in reversed(list(enumerate(myList[i]))):
newList.append(x)
I get the below output, which is close
[41.8493707892,
-87.93897686650001,
41.8471652588,
-87.93893322819997,
41.8474548975,
-87.9292710931,
41.8477438951,
-87.91960917239999,
41.8404034535,
-87.91927828050001]
But I am looking for
[(41.8493707892, -87.93897686650001),
(41.8471652588, -87.93893322819997),
(41.8474548975, -87.9292710931),
(41.8477438951, -87.91960917239999),
(41.8404034535, -87.91927828050001)]
ideally i would like to do this all in the nested for-loop; but I do not care if I have to do something with the newList to group the pairs.
As always any help is appreciated.
Unless you're really attached to the nested for-loop, it's probably easiest to use this sort of list comprehension:
newlist=[i[::-1] for i in myList]
>>> newlist
[(41.8493707892, -87.93897686650001), (41.8471652588, -87.93893322819997), (41.8474548975, -87.9292710931), (41.8477438951, -87.91960917239999), (41.8404034535, -87.91927828050001)]
You can do this very easy with list comprehension.
[(b,a) for a,b in myList]

Resources