Combing lists into list in Python - python-3.x

I have these lists in Python
text_0 = ['Weight','Weight', 'Weight']
text_1 = [['x','y','z'],['x','y','z'],['x','y','z']]
text_2 = [['1','2,','3'],['4','5','6'],['7','8','9']]
I would like would to create a new list like this
new_list = ['Weight','x','1','y','2','z','3','Weight','x','4','y','5','z','6','Weight','x','7','y','8','z','9']
How do I do this in Python?

In your case we can do with python
sum([[x] + [ items for item in zip(y,z) for items in item]
for x, y, z in zip(text_0,text_1,text_2)],[])
['Weight', 'x', '1', 'y', '2', 'z', '3', 'Weight', 'x', '4', 'y', '5', 'z', '6', 'Weight', 'x', '7', 'y', '8', 'z', '9']

Related

How to compare particular element in list python3?

l1= [['1', 'apple', '1', '2', '1', '0', '0', '0'], ['1',
'cherry', '1', '1', '1', '0', '0', '0']]
l2 = [['1', 'cherry', '2', '1'],
['1', 'plums', '2', '15'],
['1', 'orange', '2', '15'],
['1', 'cherry', '2', '1'],
['1', 'cherry', '2', '1']]
output = []
for i in l1:
for j in l2:
if i[1] != j[1]:
output.append(j)
break
print(output)
Expected Output:
[['1', 'plums', '2', '15'], ['1', 'orange', '2', '15']]
How to stop iteration and find unique elements and get the sublist?
How to stop iteration and find unique elements and get the sublist?
To find the elements in L2 that are not in L1 based on the fruit name:
l1= [[1,'apple',3],[1,'cherry',4]]
l2 = [[1,'apple',3],[1,'plums',4],[1,'orange',3],[1,'apple',4]]
output = []
for e in l2:
if not e[1] in [f[1] for f in l1]: # search by matching fruit
output.append(e)
print(output)
Output
[[1, 'plums', 4], [1, 'orange', 3]]
You can store all the unique elements from list1 in a new list, then check for list2 if that element exists in the new list. Something like:
newlist = []
for item in l1:
if item[1] not in newlist:
newlist.append(item)
output = []
for item in l2:
if item[1] not in newlist:
output.append(item)
print(output)
This is slightly inefficient but really straightforward to understand.

forming a sublist from lists

I have 3 lists:
color = ['red','orange','purple','black']
number = ['2','4','6','77']
letter = ['K','R','U','Z']
what I want to end up with is:
mylist=[['K','R','U','Z','red','2'],['K','R','U','Z','orange','4'],['K','R','U','Z','purple','6'], ['K','R','U','Z','black','77']]
I tried:
for i in range(4):
letter.append(color[i])
letter.append(number[i])
This does not give me what I need.
You can use a list comprehension for that :
color = ['red','orange','purple','black']
number = ['2','4','6','77']
letter = ['K','R','U','Z']
mylist = [ letter + [c, n] for c, n in zip(color, number) ]
print(mylist)
Not so beautiful but pretty simple in my opinion:
color = ['red', 'orange', 'purple', 'black']
number = ['2', '4', '6', '77']
letter = ['K', 'R', 'U', 'Z']
my_list = []
for i in range(len(color)):
new_list = letter[:]
my_list.append(new_list)
new_list.append(color[i])
new_list.append(number[i])
print(my_list)
Output:
[['K', 'R', 'U', 'Z', 'red', '2'], ['K', 'R', 'U', 'Z', 'orange', '4'], ['K', 'R', 'U', 'Z', 'purple', '6'], ['K', 'R', 'U', 'Z', 'black', '77']]

Assigning a new key to specific key-value pairs in a list of python dictionary

I have the following list of python dictionary:
x=[{'a': '1',
'b': '2',
'c': '3',
'd': '4',
'e': '5',
'f': '6',
'g': '7'}]
My desired output:
x=[{'g': '7', 'AA': {'a': '1', 'b': '2', 'c': '3', 'd': '4', 'e': '5', 'f': '6'}}]
I was able to assign a new key in the x list to specific key-value pairs, however, I'm appending it to the current list and getting the following output:
x=[{'a': '1',
'b': '2',
'c': '3',
'd': '4',
'e': '5',
'f': '6',
'AA':{'a':'1','b':'2','c':'3','d':'4','e':'5','f':'6'},
{'g':'7'}]
Eventually, I had to remove the keys a through f.
Current working solution:
for k in x:
k['AA'] = {u: k[u] for u in sorted(k.keys())[:6]}
rem_list = ['a', 'b', 'c', 'd', 'e', 'f']
for k in rem_list:
for v in x:
v.pop(k)
I need to simply assign a new key to the corresponding key-value pairs on the fly without looping through the list twice.
Why not just create a new dictionary? It's much safer as it doesn't manipulate mutable dict data, and may cause other methods using this dict to mis-behave.
Try something like:
x=[{'a': '1',
'b': '2',
'c': '3',
'd': '4',
'e': '5',
'f': '6',
'g': '7'}]
new_dict = {'AA': {k: v for k, v in x[0].items() if k != 'g'}, 'g': x[0]['g']}

Concatenat very third List items into one item

Hi am trying to concatenate every third item into one item:
I have :
dic = ['3', '3', '⛂', '3', '4', '⛀', '4', '3', '⛀', '4', '4', '⛂']
what i would like is to find a way to create a list of list with every third item
like
dic = [['3', '3', '⛂'], ['3', '4', '⛀'], ['4', '3', '⛀'], ['4', '4', '⛂']]
Here you go!
dic = ['3', '3', '⛂', '3', '4', '⛀', '4', '3', '⛀', '4', '4', '⛂']
lst=[]
for i in range(0,len(dic),3):
lst.append(dic[i:i+3])
print(lst)
Output:
[['3', '3', '⛂'], ['3', '4', '⛀'], ['4', '3', '⛀'], ['4', '4', '⛂']]

List to string to lists of letter

I am trying to convert a list of strings to a list of letters/numbers but keeping the length of list the same. Here is my list look like,
a = ["0587828028", "2967480535"]
My code to convert the above list to split the string and save in a new list.
new_a = []
for i in range(len(a)):
new_a += a[i]
And the output is on list,
['0', '5', '8', '7', '8', '2', '8', '0', '2', '8', '2', '9', '6', '7', '4', '8', '0', '5', '3', '5']
Desired output should be 2 list:
['0', '5', '8', '7', '8', '2', '8', '0', '2', '8'] ['2', '9', '6', '7', '4', '8', '0', '5', '3', '5']
Any suggestion is much appreciated, I am very new in python.
Just use the built-in list() iterable expansion:
a = ["0587828028", "2967480535"]
new_a = [list(x) for x in a]
# [['0', '5', '8', '7', '8', '2', '8', '0', '2', '8'],
# ['2', '9', '6', '7', '4', '8', '0', '5', '3', '5']]

Resources