Python 2.7: update element in a list [duplicate] - python

This question already has answers here:
Finding and replacing elements in a list
(10 answers)
Closed 4 years ago.
I would like to update an element from a list.
This is my kind of list:
list= ["toto", "stack" , "element_to_update", "overflow"]
I want to update this list when "toto" and "stack" found as element[0] and element[1] to have my list updated (and concatenate with existing element[2])
The list that I want at the end:
list=["toto", "stack", "element_to_update_is_updated", "overflow"]
What is the best way to do that?
Thanks in advance for your help.

Why not:
l = ["toto", "stack", "element_to_update", "overflow"]
if l[:2] == ['toto','stack']:
l[2] += '_is_updated'
And now:
print(l)
Is:
['toto', 'stack', 'element_to_update_is_updated', 'overflow']

Related

making sublist from sublist based on index position python [duplicate]

This question already has an answer here:
How to zip lists in a list [duplicate]
(1 answer)
Closed 3 years ago.
I have a list
mainlist=[[1,2,3,4],['a','b','c','d'],[4,5,6,7]]
want to make sublist based on index position in sublist. The output is
finallist=[[1,'a',4],[2,'b',5],[3,'c',6],[4,'d',7]]
Thanks
Use zip with unpacking:
finallist = list(map(list, zip(*mainlist)))

python append to nested list , index out of range [duplicate]

This question already has answers here:
How to create a number of empty nested lists in python [duplicate]
(2 answers)
Closed 3 years ago.
I am trying to create a nested list. The result would be something like [[0,1],[2,3],[0,4]] I tried the following and got an index out of range error:
list = []
list[0].append(0)
Is it not appending 0 to the first item in the list? How should I do this? Many thanks for your help.
A little typo, you should do:
list = [[]]
list[0].append(0)
You need to have a first element first...
Edit:
Use:
list = []
for i in range(3):
list.append([])
list[-1].append(0)
For that you'll need to append a list to a list first, i.e.:
list = []
list.append([])
list[0].append(0)
print(list)
# [[0]]
lst = []
lst.append([0,1])
lst.append([2,3])
lst.append([0,4])
print(lst)
How about this ? Or you need in the form of loop with constant set of numbers?

Remove Multiple Same Item From List Without Using Set [duplicate]

This question already has answers here:
Remove all occurrences of a value from a list?
(26 answers)
Closed 4 years ago.
How can I remove several same items in a LIST
for ex:
a = [1,1,1,1,1,1,2,2,2,2,2,2]
I want to remove all 1 values, such that output is:
a = [2,2,2,2,2,2]
I tried a.remove(1) but it only remove one '1' at the first encounter.
i try looking for comprehension method
Try it with a list comprehension:
a = [1,1,1,1,1,1,2,2,2,2,2,2]
cleaned_a = [el for el in a if el != 1]

remove repeated elements in a python list [duplicate]

This question already has answers here:
Removing duplicates in lists
(56 answers)
Closed 5 years ago.
I have a list of strings in which there are a lot of repeated items. I would like to make a new list in which all items are present but there is only one occurrence of each item.
input:
mylist = ["hg", "yt", "hg", "tr", "yt"]
output:
newlist = ["hg", "yt", "tr"]
I actually have tried this code but did not return what I want:
newlist = []
for i in range(len(mylist)):
if mylist[i+1] == mylist[i]:
newlist.append(mylist[i])
You can simply use a set:
newlist = set(mylist)
Or, to retrieve exactly a list, but is can be useless depending what you are doing with:
nexlist = list(set(mylist))

How to convert a python list containing several lists into single list? [duplicate]

This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 6 years ago.
How do I convert a list of lists to a single list?
Input:
a=[['AA'], ['AE'], ['AH'], ['AO'],]
Desired output:
['AA','AE','AH','AO']
a=[['AA'], ['AE'], ['AH'], ['AO'],]
l=[]
for i in a:
l.extend(i)
print l
you could use comprehension list:
or using map and lambda functions
a=[['AA'], ['AE'], ['AH'], ['AO'],]
# open the item [0] with generators
Newa = [x[0] for x in a]
>>>['AA', 'AE', 'AH', 'AO']
see examples: http://www.secnetix.de/olli/Python/list_comprehensions.hawk
EDIT:
for i, value in enumerate(a):
a[i] = value[0]

Categories

Resources