Python: split one element in list [duplicate] - python

This question already has answers here:
How do I split a string into a list of characters?
(15 answers)
Changing one element in a list from string to integer
(1 answer)
Closed 1 year ago.
Can not solve problem i have following list ['882','200','60'] sometimes it can be ['882','200'] i want get list like this [['8','8','2'], '200', '60'].
Another words no matter how many items are in the list, we always split the first item

You can use enumerate to get the first element:
l = ['882','200','60']
[e if i else list(e) for i,e in enumerate(l)]
output:
[['8', '8', '2'], '200', '60']
other examples
>>> [e if i else list(e) for i,e in enumerate(['123'])]
[['1', '2', '3']]
>>> [e if i else list(e) for i,e in enumerate(['882','200'])]
[['8', '8', '2'], '200']

mylist = ['882','200','60']
mylist[0] = list(mylist[0])

Related

Delete even indexes out of range [duplicate]

I'm trying to remove the odd-indexed elements from my list (where zero is considered even) but removing them this way won't work because it throws off the index values.
lst = ['712490959', '2', '623726061', '2', '552157404', '2', '1285252944', '2', '1130181076', '2', '552157404', '3', '545600725', '0']
def remove_odd_elements(lst):
i=0
for element in lst:
if i % 2 == 0:
pass
else:
lst.remove(element)
i = i + 1
How can I iterate over my list and cleanly remove those odd-indexed elements?
You can delete all odd items in one go using a slice:
del lst[1::2]
Demo:
>>> lst = ['712490959', '2', '623726061', '2', '552157404', '2', '1285252944', '2', '1130181076', '2', '552157404', '3', '545600725', '0']
>>> del lst[1::2]
>>> lst
['712490959', '623726061', '552157404', '1285252944', '1130181076', '552157404', '545600725']
You cannot delete elements from a list while you iterate over it, because the list iterator doesn't adjust as you delete items. See Loop "Forgets" to Remove Some Items what happens when you try.
An alternative would be to build a new list object to replace the old, using a list comprehension with enumerate() providing the indices:
lst = [v for i, v in enumerate(lst) if i % 2 == 0]
This keeps the even elements, rather than remove the odd elements.
Since you want to eliminate odd items and keep the even ones , you can use a filter as follows :
>>>filtered_lst=list(filter(lambda x : x % 2 ==0 , lst))
this approach has the overhead of creating a new list.

How to append element-wise of two lists in Python, where at least one of them is string [duplicate]

This question already has answers here:
Interleave multiple lists of the same length in Python [duplicate]
(11 answers)
Closed 2 years ago.
I would like to append element-wise of two lists (one in a string format). Suppose:
a = [1,2,3]
b = ['&','&','\\']
The output that I am looking is:
['1', '&', '2', '&', '3', '\\']
I tried the below code, but the answer is not the right one:
[str(x)+y for x,y in zip(a,b)]
Output: ['1&', '2&', '3\\']
Try this :
[k for i in zip(map(str, a), b) for k in i]
Output :
['1', '&', '2', '&', '3', '\\']
One limitation of using zip here would be it would iterate up to length of the smallest list, either a or b. For cases like the following :
a = [1, 2, 3, 4]
b = ['&','&','\\']
the above code would produce same result ['1', '&', '2', '&', '3', '\\'] and won't bother to go beyond 3 elements. To circumvent this, you can use zip_longest.

Individually add each entry in a list to a string [duplicate]

This question already has answers here:
Appending the same string to a list of strings in Python
(12 answers)
Closed 4 years ago.
Nums = ['1', '2', '3', '4']
print(str('\n'.join(Nums)) + 'XXX')
Currently this returns
1
2
3
4XXX
I'd like to get the code to return xxx after each number rather than after only the last! Is there a way to process each one of these individually so it prints each entry in Nums + XXX?
Example:
1XXX
2XXX
...
Thanks!
In [1]: Nums = ['1', '2', '3', '4']
In [2]: print('\n'.join([i+'XXX' for i in Nums]))
1XXX
2XXX
3XXX
4XXX
To print each line separately:
In [5]: for i in Nums:
...: print('{}XXX\n'.format(i))
...:
1XXX
2XXX
3XXX
4XXX
It's similar to what you did with a small modification.
Nums = ['1', '2', '3', '4']
print('XXX\n'.join(Nums) + 'XXX\n')
I hope this helps!.

How do I remove hyphens from a nested list?

In the nested list:
x = [['0', '-', '3', '2'], ['-', '0', '-', '1', '3']]
how do I remove the hyphens?
x = x.replace("-", "")
gives me AttributeError: 'list' object has no attribute 'replace', and
print x.remove("-")
gives me ValueError: list.remove(x): x not in list.
x is a list of lists. replace() will substitute a pattern string for another within a string. What you want is to remove an item from a list. remove() will remove the first occurrence of an item. A simple approach:
for l in x:
while ("-" in l):
l.remove("-")
For more advanced solutions, see the following: Remove all occurrences of a value from a Python list

Combine elements in a nested list - Python 3 [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Python3 Concatenating lists within lists
I have
Nested_List = [['John', 'Smith'], ['1', '2', '3', '4']]
I want to manipulate this list such that I get this output:
Nested_List = [['John Smith'], ['1', '2', '3', '4']]
Basically the two elements in the first list were combined. How do I do this?
newElement = [' '.join(NestedList[0])]
Nested_List[0] = newElement
(modifies list, bad practice, but simple)

Categories

Resources