How to Remove Additional Commas in a List in Python

How to remove additional commas in a list in python

x = ['apples', ',', 'orange', ',', 'grapes']

x = list(filter(lambda v: v != ',', x))

print(x)

another alternative:

x = [v for v in x if v != ',']

results in:

['apples', 'orange', 'grapes']

How to remove the extra commas from the list of email addresses

No need for regular expressions. Use .split(',') to split into a list of strings.

email_lst = email_addr.split(',')

Then join with comma, but filter out blank values

email_addr2 = ",".join(e.strip() for e in email_lst if e.strip())
# 'test@domain.com,test1@domain.com,test2@domain.com'

In Python 3.8+, you can use the walrus operator to avoid calling .strip() twice:

email_addr2 = ",".join(e for ee in email_lst if (e := ee.strip()))

Removing all commas from list in Python

I would use the following:

# Get an array of numbers
numbers = map(float, '1,2,3,4'.split(','))

# Now get the sum
total = sum(numbers)

Is there a way to remove all comma's in a list?

You could use a list comprehension, and extract integers from the strings using string.replace to remove all the ',':

l = ['10', '1,000', '51,000', '500', '63,000']
[int(s.replace(',','')) for s in l]
# [10, 1000, 51000, 500, 63000]

how to remove a comma from a list

Also you can use: str(my_list1).replace( ',' , ' ' )

Python how to remove unwanted commas in a dataframe containing lists as element

df['data'] = df.data.replace(to_replace=r',,', value='', regex=True)
print(df)

get rid of commas in a list python

If you want a numpy ndarray, use:

np.array([1, 2, 4, 7, 5, 2])


Related Topics



Leave a reply



Submit