List resolver with tuples - python

I have a list:
List = [('4022-a751',), ('0bfc-4d53',)]
And want to resolve it into the output below:
Output = ['4022-a751','0bfc-4d53']

You should read about List Comprehensions in Python
list_ = [('4022-a751',), ('0bfc-4d53',)]
res = [x for item in list_ for x in item]
Output
['4022-a751', '0bfc-4d53']

A tuple can be manipulated like an array with index.
input_arr = [('4022-a751',), ('0bfc-4d53',)]
output_arr = [a[0] for a in input_arr]
print(output_arr)

You can use this.
old_list= [('4022-a751',), ('0bfc-4d53',)]
new_list = [''.join(i) for i in old_list]
print(new_list)

Related

Slice all strings in a list from their first '\n'

How to remove first RemoveThisX\n from list
['RemoveThis1\nDontRemove\nDontRemove','RemoveThis2\nDontRemove\nDontRemove', 'RemoveThis3\nDontRemove\nDontRemove', 'RemoveThis4\nDontRemove\nDontRemove']
Trying to remove RemoveThis1\n, RemoveThis2\n, RemoveThis3, RemoveThis4\n
Final result need to be
['DontRemove\nDontRemove','DontRemove\nDontRemove', 'DontRemove\nDontRemove', 'DontRemove\nDontRemove']
a_list = ['RemoveThis1\nDontRemove\nDontRemove','RemoveThis2\nDontRemove\nDontRemove', 'RemoveThis3\nDontRemove\nDontRemove', 'RemoveThis4\nDontRemove\nDontRemove']
result = [item[item.find('\n')+1:] for item in a_list]
print(result)
['DontRemove\nDontRemove', 'DontRemove\nDontRemove', 'DontRemove\nDontRemove', 'DontRemove\nDontRemove']
test_list = ['RemoveThis1\nDontRemove\nDontRemove','RemoveThis2\nDontRemove\nDontRemove', 'RemoveThis3\nDontRemove\nDontRemove', 'RemoveThis4\nDontRemove\nDontRemove']
result = ["\n".join(item.split("\n")[1:]) for item in test_list]
print(result)
Output will be:
['DontRemove\nDontRemove', 'DontRemove\nDontRemove', 'DontRemove\nDontRemove', 'DontRemove\nDontRemove']
assuming:
initial_list = ['RemoveThis1\nDontRemove\nDontRemove','RemoveThis2\nDontRemove\nDontRemove', 'RemoveThis3\nDontRemove\nDontRemove', 'RemoveThis4\nDontRemove\nDontRemove']
I would recommend using either the map function:
mapped_list = list(map(lambda x: x[x.find('\n') + 1:], initial_list))
or list comprehension:
comprehended_list = [string[string.find('\n') + 1:] for string in initial_list]
Both should produce the asked list.

Selecting 1st element of every list within list

I have the following list of lists with multiple elements:
list = [[1633425661439, 0.11643042583898743],
[1633428739018, 0.11682454707026001],
[1633432086311, 0.11950356856187618]]
I want to populate a new_list1 and new_list2 with the first and second numbers within each of those lists, respectively, yielding:
new_list1 = [1633425661439,
1633428739018,
1633432086311]
And:
new_list2 = [0.11643042583898743,
0.11682454707026001,
0.11950356856187618]
I tried:
for n in list:
for i in n:
new_list1.append(i[0])
new_list2.append(i[1])
But got: TypeError: 'int' object is not subscriptable
You can try something
list_ = [[1633425661439, 0.11643042583898743],
[1633428739018, 0.11682454707026001],
[1633432086311, 0.11950356856187618]]
list_a = [first[0] for first in list_]
list_b = [first[1] for first in list_]
Other way
new_list1 = []
new_list2 = []
for inner_list in list_:
new_list1.append(inner_list[0])
new_list2.append(inner_list[1])
You can transpose it like this:
lst = [[1633425661439, 0.11643042583898743],
[1633428739018, 0.11682454707026001],
[1633432086311, 0.11950356856187618]]
new_list_1, new_list_2 = map(list, zip(*lst))
And if you are ok with tuples instead of lists, the following will do:
new_list_1, new_list_2 = zip(*lst)
And you really should not name a variable list. It shadows the built-in type.
You can also use simple comprehensions:
new_list_1 = [a for a, _ in lst]
new_list_2 = [a for _, a in lst]
Some docs:
map
zip
You have one level of nesting too much, this would
for n in list:
for i in n:
print(i)
would print single elements, which are numbers, you need to do
for n in list:
new_list1.append(n[0])
new_list2.append(n[1])
As side note, please avoid using list as it is already used name in python. Overshadowing it might cause unexpected behavior, you can use lst name i.e.:
lst = [[1,2],[3,4],[5,6]]
new_lst1 = []
new_lst2 = []
for n in lst:
new_lst1.append(n[0])
new_lst2.append(n[1])
print(new_lst1)
print(new_lst2)
output
[1, 3, 5]
[2, 4, 6]
you can unpack the first and second number in the for loop itself.
(BTW best not call the variable "list" because it is the same as a python build-in)
list_ = [[1633425661439, 0.11643042583898743],
[1633428739018, 0.11682454707026001],
[1633432086311, 0.11950356856187618]]
new_list1 = []
new_list2 = []
for (i, j) in l:
new_list1.append(i)
new_list2.append(j)
Following PEP-8 code style guideline please do not name the variables with reserved keywords like list, dict, for, etc.
With the second loop you iterate over int numbers within the inner lists.
If you need to use only the first 2 elements of each list, one loop os enough:
list_ = [
[1633425661439, 0.11643042583898743],
[1633428739018, 0.11682454707026001],
[1633432086311, 0.11950356856187618]]
for inner_list in list_:
new_list1.append(inner_list[0])
new_list2.append(inner_list[1])
list= [[1633425661439, 0.11643042583898743], [1633428739018, 0.11682454707026001], [1633432086311, 0.11950356856187618]]
new_list1 = [ ]
new_list2 = [ ]
for inner_list in list:
new_list1.append(inner_list[0])
new_list2.append(inner_list[1])
print(new_list1)
print(new_list2)

Create a string list by combining lists and strings

I'm trying to create a list that is combining two lists and some strings:
string = "test"
list1 = ["1","2","3"]
list2 = ["a","b","c"]
result = ["test.1.a","test.2.b","test.3.b"]
I tried messing around with .join and zip functions to no avail.
I believe that a pythonic way could be the following, using zip and list comprehension:
output = ["{}.{}.{}".format(string, a, b) for a, b in zip(list1, list2)]
This works:
['.'.join((string,)+i) for i in zip(list1, list2)]
Output:
['test.1.a', 'test.2.b', 'test.3.c']
string = "test"
list1 = ["1","2","3"]
list2 = ["a","b","c"]
lst = ['{}.{}.{}'.format(string, list1[x], list2[x]) for x in range(len(list1))]
print(lst)
Output
['test.1.a', 'test.2.b', 'test.3.c']
Simply use concatenation trick:
string1 = "test"
list1 = ["1","2","3"]
list2 = ["a","b","c"]
result = [(string1+"."+list1[i]+"."+list2[i]) for i in range(len(list1))]
print(result)
Output:
['test.1.a', 'test.2.b', 'test.3.c']
List Comprehension:
string = "test"
list(map(lambda x,y: string+"."+x+"."+y, list1,list2))
Output:
['test.1.a', 'test.2.b', 'test.3.c']

How to make a list with elements to list with list with only one element?

I have a list with n elements but i would like to convert it to a list which contains n list, and every list contains a single element.
a = ['1','2','3','4']
b = [['1'],['2'],['3'],['4']]
How can I make from a to b?
You can try list comprehension
b = [[i] for i in a]
You can use map:
b = list(map(lambda x: [x], a))
or a list comprehension:
b = [[i] for i in a]
You can iterate through the a list and append new list with the item to b list.
a = ['1','2','3','4']
b = []
for i in range(len(a)):
b.append([a[i]])
print(b)
This is basically the same solution as the one from JRodDynamite, but more readable for beginners.
Very Simple Solution Could be from generators
a = ['1','2','3','4']
b = [[item] for item in a]

Adding a value to a list of lists

I want to add a value to a list of lists.
For input of [[1,2],[2,3]]
I want output of [[2,3],[3,4]]
I can do it with loops:
list_of_lists = [[1,2],[2,3]]
output = []
for list in list_of_lists:
sub_output = []
for value in list:
sub_output.append(value+1)
output.append(sub_output)
print(output)
Can I do this with list comprehension?
If I do:
[value + 1 for list in list_of_lists for value in list]
It gives me [2,3,3,4]. Can I get it to keep the sublist format somehow?
Try...
[ [n + 1 for n in inner_list] for inner_list in list ]
Yeah, you need a nested comprehension:
[[item + 1 for item in list] for list in list_of_lists]
Another way would be to use map:
map(lambda l: map(lambda i: i + 1, l), list_of_lists)
You need to nest a comprehension into that comprehension. Unpack each sublist to make it easier.
[[a+1, b+1] for a,b in list_of_lists]

Categories