Appending strings with parentheses in a list - string

I have a set of strings.
people = {'RAM_S', 'SHYAM', 'GEORGEY', 'MUFASSIR'}
I have an empty list : list_c = [].
I want to append items in people to list_c such that resulting list looks as follows:
list_c = [('RAM_S'),('SHYAM'),('GEORGEY'),('MUFASSIR')]
I'm unable to append elements with parentheses in the list.
Please suggest some way to do it.

If you need tuples:
result = [tuple([i]) for i in people]
OUTPUT:
[('MUFASSIR',), ('SHYAM',), ('GEORGEY',), ('RAM_S',)]

Related

How to combine elements in set and a string to a list in python?

Have got a set() like
item = {'Apple','Lemon'}
and a string flow='Mango'
need to combine both to form a list like below
result = ['Mango','Apple','Lemon']
tried the code result = [flow ,item] which doesn't work out.
Help me through this. Thanks!
You can unpack the set into a new list that includes flow:
result = [flow, *item]
Add item to the set, conver the set to a list:
item.add(flow)
list(item)
# ['Apple', 'Mango', 'Lemon']
Or convert the set to a list and concatenate it with a list of flow:
[flow] + list(item)
# ['Mango', 'Apple', 'Lemon']
The second approach preserves the original set.

Convert list of str to dict using list comprehension

I have a list of strings represented as dictionaries and I want to convert them to dictionaries using list comprehension. my data looks like this:
['{"A":"1"}','{"B":"2"}']
The output should be:
[{"A":"1"},{"B":"2"}]
I tried this:
dict_var = [i for i in list_var]
And it's just returning the same result without any change!
This can be achieved by using json.loads(i) and in a list comprehension such as:
dict_var = [json.loads(i) for i in list_var]

How can I group a list of strings by another list of strings using Python?

I have two lists:
List 1
filenames = ['K853.Z', 'K853.N', 'K853.E', 'K400.Z', 'K400.N', 'K400.E']
List 2
l = ['K853', 'K400']
I want to iterate through the filenames list and group the strings by the l list.
I've tried the below:
for name in filenames:
for i in l:
if i in name:
print(name)
But this just prints the first list. I've seen the Pandas groupby method but I can't figure out how to utilize it in this case.
you could just use a list generator like this:
new = [[name for name in filenames if(name.startswith(prefix))] for prefix in l]
This would provide you with a list of list, where for each index of l you would get a list of files with its prefix at the same index in the new list.

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]

Python 3.2 split the string values in a list

I have a list like below:
rawinput = ['corp\\asre', 'corp\\banjar', 'corp\\bicknk', 'corp\\daniele']
I want to be able to do
users = []
users = rawinput.split(",")
print(users)
How do I do this in Python 3.2? Thanks.
What you have,
rawinput = ['corp\\asre', 'corp\\banjar', 'corp\\bicknk', 'corp\\daniele']
is a list of strings already. You can just iterate through it as a list. You don't need to split anything.
If you had something like this,
rawinput = "corp\\asre, corp\\banjar, corp\\bicknk, corp\\daniele"
rawinput.split(',') would return the above list.
split() is applied on string, in return it gives you a list[] which contains the substring as elements in order of the parent string.
In your case:
input = "corp\\asre, corp\\banjar, corp\\bicknk, corp\\daniele"
input.split(',')
will return
['corp\\asre', 'corp\\banjar', 'corp\\bicknk', 'corp\\daniele']

Resources