Append something into interior list within nested list [duplicate] - python

This question already has answers here:
How to insert into python nested list
(3 answers)
Closed 5 years ago.
so I have a nested list like this:
nested list=[['Bike No.,Purchase Date,Batt %,Last Maintenance,KM since Last'], ['T101,10/04/2016,55,10/01/2017,25.08'], ['T102,01/07/2016,10,15/05/2017,30.94'], ['T103,15/11/2016,94,13/06/2017,83.16'], ['T104,25/04/2017,58,10/01/2017,25.08'], ['T105,24/05/2017,5,20/06/2017,93.80']]
and I want to append another element into this list ['Bike No.,Purchase Date,Batt %,Last Maintenance,KM since Last'] ,how should I do it?
This list is originally created from a csv file and is split by its line followed by its elements

by coldspeed:
nested_list[0].append(...)

Related

Create a long list of characters (not numbers) with list comprehension [duplicate]

This question already has answers here:
Python: Short way of creating a sequential list with a prefix
(3 answers)
Create a list of strings with consecutive numbers appended
(6 answers)
Appending the same string to a list of strings in Python
(12 answers)
Closed 7 months ago.
In Python how can I need to create a long list that I'm trying to avoid typing, the list looks lis this
brands = ['_1','_2','_3'... '_998']
I can create the list of numbers with a for loop, but I'm trying to use list comprehension for the characters which should be faster.
Thanks!
my list=["_" + str(i) for i in range(1,999)]

How to convert list object to string list [duplicate]

This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 10 months ago.
I have problem with converting
[['a'],['b'],['c']] --> ['a','b','c']
it seems simple but I am struggling for hours...
You could use a list comprehension to do something as simple as
[element[0] for element in letters_list]
This remakes the list, extracting the 0-index element from each sublist

How to convert string without delimiter to list in python [duplicate]

This question already has answers here:
How do I split a list into equally-sized chunks?
(66 answers)
Closed 3 years ago.
So I have string like this:
"UFFKTEWKW"
And I need to convert it to a list or tuple like this:
("UFF", "KTE", "WKW")
So every 3 letters from the string goes to separate element of list or tuple.
I can't use split() here because string doesn't have any delimiters. I don't wanna make some dummy for cycle for it. And I think there should be simple solution for it.
You can use this.
a="UFFKTEWKW"
out=tuple(a[i:i+3] for i in range(0,len(a),3))
# ('UFF', 'KTE', 'WKW')

How could I remove adjacent duplicate value without using a loop? [duplicate]

This question already has answers here:
Remove adjacent duplicate elements from a list [duplicate]
(17 answers)
Closed 3 years ago.
Like the given list is
l=[4,2,3,4,4,5]
Turn into
l=[4,2,3,4,5]
I try to create an empty list to store the value but it is hard to compare if the two elements are equal with no loop
to store every element only once is same like a set.
you can use following code.
l=[2,3,4,4,5]
l=list(set(l))
l.sort()
it will give your expected output
You can use a dictionary which only keeps unique values as its key
l=[2,3,4,4,5]
list(dict.fromKeys(l))

Key-value map in python [duplicate]

This question already has answers here:
How can I make a dictionary (dict) from separate lists of keys and values?
(21 answers)
Closed 7 years ago.
I have two list of same length l1=[reboot,revision,disk_space] and l2=[no,12.15,4300] I want to make these two list as key-value map. Output should be like this [reboot:no,revision:12.18,disk_space:4300].. I tried Zip it is not working
You can create a dictionary from the output of zip (Thanks, Martijn!):
print dict(zip(l1, l2))

Categories

Resources