All Questions

Tagged with
Filter by
Sorted by
Tagged with
2095 votes
10 answers
1.5m views

Why is it string.join(list) instead of list.join(string)?

This has always confused me. It seems like this would be nicer: ["Hello", "world"].join("-") Than this: "-".join(["Hello", "world"]) Is ...
Evan Fosmark's user avatar
1186 votes
3 answers
3.8m views

How to convert list to string [duplicate]

How can I convert a list to a string using Python?
Nm3's user avatar
  • 12k
1174 votes
12 answers
2.2m views

How to concatenate (join) items in a list to a single string

How do I concatenate a list of strings into a single string? For example, given ['this', 'is', 'a', 'sentence'], how do I get "this-is-a-sentence"? For handling a few strings in separate ...
alvas's user avatar
  • 118k
978 votes
14 answers
1.1m views

Remove empty strings from a list of strings

I want to remove all empty strings from a list of strings in python. My idea looks like this: while '' in str_list: str_list.remove('') Is there any more pythonic way to do this?
zerodx's user avatar
  • 9,801
892 votes
21 answers
723k views

How to convert string representation of list to a list

I was wondering what the simplest way is to convert a string representation of a list like the following to a list: x = '[ "A","B","C" , " D"]' Even in cases ...
harijay's user avatar
  • 11.6k
890 votes
3 answers
992k views

C# List<string> to string with delimiter

Is there a function in C# to quickly convert some collection to string and separate values with delimiter? For example: List<string> names --> string names_together = "John, Anna, Monica"
nan's user avatar
  • 20k
871 votes
17 answers
1.9m views

How to check if a string is a substring of items in a list of strings

How do I search for items that contain the string 'abc' in the following list? xs = ['abc-123', 'def-456', 'ghi-789', 'abc-456'] The following checks if 'abc' is in the list, but does not detect 'abc-...
SandyBr's user avatar
  • 11.8k
732 votes
23 answers
588k views

Java: convert List<String> to a join()d String

JavaScript has Array.join() js>["Bill","Bob","Steve"].join(" and ") Bill and Bob and Steve Does Java have anything like this? I know I can cobble something ...
Jason S's user avatar
  • 187k
704 votes
15 answers
868k views

How would you make a comma-separated string from a list of strings?

What would be your preferred way to concatenate strings from a sequence such that between every two consecutive pairs a comma is added. That is, how do you map, for instance, ['a', 'b', 'c'] to 'a,b,c'...
mweerden's user avatar
  • 13.8k
687 votes
16 answers
1.2m views

How do I split a string into a list of characters?

How do I split a string into a list of characters? str.split does not work. "foobar" → ['f', 'o', 'o', 'b', 'a', 'r']
Adrian's user avatar
  • 7,881
680 votes
11 answers
1.1m views

Alphabet range in Python

How do I create a list of alphabet characters, without doing it manually like this? ['a', 'b', 'c', 'd', ..., 'z']
Alexa Elis's user avatar
  • 7,389
651 votes
10 answers
2.0m views

How do I split a string into a list of words?

How do I split a sentence and store each word in a list? e.g. "these are words" ⟶ ["these", "are", "words"] To split on other delimiters, see Split a ...
Thanx's user avatar
  • 7,583
560 votes
14 answers
1.2m views

How to initialize a list of strings (List<string>) with many string values

How is it possible to initialize (with a C# initializer) a list of strings? I have tried with the example below but it's not working. List<string> optionList = new List<string> { "...
Bilgin Kılıç's user avatar
414 votes
15 answers
580k views

Determine if string is in list in JavaScript

In SQL we can see if a string is in a list like so: Column IN ('a', 'b', 'c') What's a good way to do this in JavaScript? It's so clunky to do this: if (expression1 || expression2 || str === 'a' || ...
ErikE's user avatar
  • 49.6k
406 votes
3 answers
378k views

Splitting on last delimiter in Python string?

What's the recommended Python idiom for splitting a string on the last occurrence of the delimiter in the string? example: # instead of regular split >> s = "a,b,c,d" >> s.split(",") >&...
user avatar
386 votes
9 answers
541k views

How can I convert each item in the list to string, for the purpose of joining them? [duplicate]

I need to join a list of items. Many of the items in the list are integer values returned from a function; i.e., myList.append(munfunc()) How should I convert the returned result to a string in ...
AKM's user avatar
  • 6,375
332 votes
5 answers
425k views

string.join(list) on object array rather than string array

In Python, I can do: list = ['a', 'b', 'c'] ', '.join(list) # 'a, b, c' However, if I have a list of objects and try to do the same thing: class Obj: def __str__(self): return 'name' ...
Mat's user avatar
  • 84.2k
330 votes
3 answers
260k views

str.startswith with a list of strings to test for

I'm trying to avoid using so many comparisons and simply use a list, but not sure how to use it with str.startswith: if link.lower().startswith("js/") or link.lower().startswith("...
Eternity's user avatar
  • 3,365
316 votes
7 answers
208k views

Check if string ends with one of the strings from a list

What is the pythonic way of writing the following code? extensions = ['.mp3','.avi'] file_name = 'test.mp3' for extension in extensions: if file_name.endswith(extension): #do stuff I ...
TheMeaningfulEngineer's user avatar
306 votes
15 answers
559k views

Convert a list to a string in C#

How do I convert a list to a string in C#? When I execute toString on a List object, I get: System.Collections.Generic.List`1[System.String]
IAdapter's user avatar
  • 63.3k
269 votes
11 answers
392k views

How can I convert comma separated string into a List<int> [duplicate]

string tags = "9,3,12,43,2" List<int> TagIds = tags.Split(','); This doesn't work cause the split method returns a string[]
nacho10f's user avatar
  • 5,846
258 votes
15 answers
377k views

How do I check if there are duplicates in a flat list?

For example, given the list ['one', 'two', 'one'], the algorithm should return True, whereas given ['one', 'two', 'three'] it should return False.
teggy's user avatar
  • 6,145
253 votes
5 answers
512k views

Split a string by a delimiter in Python

How to split this string where __ is the delimiter MATCHES__STRING To get an output of ['MATCHES', 'STRING']? For splitting specifically on whitespace, see How do I split a string into a list of ...
Hulk's user avatar
  • 33.5k
235 votes
20 answers
455k views

Removing a list of characters in string

I want to remove characters in a string in python: string.replace(',', '').replace("!", '').replace(":", '').replace(";", '')... But I have many characters I have to remove. I thought about a list ...
Laura's user avatar
  • 4,269
221 votes
9 answers
111k views

case-insensitive list sorting, without lowercasing the result?

I have a list of strings like this: ['Aden', 'abel'] I want to sort the items, case-insensitive. So I want to get: ['abel', 'Aden'] But I get the opposite with sorted() or list.sort(), because ...
user avatar
220 votes
9 answers
590k views

What exactly does the .join() method do?

I'm pretty new to Python and am completely confused by .join() which I have read is the preferred method for concatenating strings. I tried: strid = repr(595) print array.array('c', random.sample(...
Matt McCormick's user avatar
219 votes
12 answers
197k views

Case insensitive 'in'

I love using the expression if 'MICHAEL89' in USERNAMES: ... where USERNAMES is a list. Is there any way to match items with case insensitivity or do I need to use a custom method? Just ...
RadiantHex's user avatar
  • 25.3k
208 votes
6 answers
573k views

Find and replace string values in list [duplicate]

I got this list: words = ['how', 'much', 'is[br]', 'the', 'fish[br]', 'no', 'really'] What I would like is to replace [br] with some fantastic value similar to <br /> and thus getting a new ...
Eric Herlitz's user avatar
  • 25.7k
194 votes
19 answers
356k views

Best way to concatenate List of String objects? [duplicate]

What is the best way to concatenate a list of String objects? I am thinking of doing this way: List<String> sList = new ArrayList<String>(); // add elements if (sList != null) { ...
Jagmal's user avatar
  • 5,866
193 votes
14 answers
741k views

How to convert a string with comma-delimited items to a list in Python?

How do you convert a string into a list? Say the string is like text = "a,b,c". After the conversion, text == ['a', 'b', 'c'] and hopefully text[0] == 'a', text[1] == 'b'?
Clinteney Hui's user avatar
187 votes
3 answers
404k views

Best way to convert list to comma separated string in java [duplicate]

I have Set<String> result & would like to convert it to comma separated string. My approach would be as shown below, but looking for other opinion as well. List<String> slist = new ...
Mad-D's user avatar
  • 4,589
182 votes
9 answers
142k views

Join a list of strings in python and wrap each string in quotation marks

I've got: words = ['hello', 'world', 'you', 'look', 'nice'] I want to have: '"hello", "world", "you", "look", "nice"' What's the easiest way to do this with Python?
kadrian's user avatar
  • 4,889
177 votes
4 answers
420k views

Apply function to each element of a list [duplicate]

Suppose I have a list like: mylis = ['this is test', 'another test'] How do I apply a function to each element in the list? For example, how do I apply str.upper to get: ['THIS IS TEST', 'ANOTHER ...
shantanuo's user avatar
  • 32.1k
169 votes
5 answers
268k views

How can I order a List<string>?

I have this List<string>: IList<string> ListaServizi = new List<string>(); How can I order it alphabetically and ascending?
markzzz's user avatar
  • 47.7k
164 votes
18 answers
381k views

Python: how to print range a-z?

1. Print a-n: a b c d e f g h i j k l m n 2. Every second in a-n: a c e g i k m 3. Append a-n to index of urls{hello.com/, hej.com/, ..., hallo.com/}: hello.com/a hej.com/b ... hallo.com/n
hhh's user avatar
  • 51.7k
163 votes
22 answers
347k views

Create nice column output in python

I am trying to create a nice column list in python for use with commandline admin tools which I create. Basicly, I want a list like: [['a', 'b', 'c'], ['aaaaaaaaaa', 'b', 'c'], ['a', 'bbbbbbbbbb', '...
xeor's user avatar
  • 5,371
155 votes
5 answers
386k views

Finding a substring within a list in Python [duplicate]

Background: Example list: mylist = ['abc123', 'def456', 'ghi789'] I want to retrieve an element if there's a match for a substring, like abc Code: sub = 'abc' print any(sub in mystring for mystring in ...
frankV's user avatar
  • 5,423
151 votes
9 answers
141k views

Pandas DataFrame stored list as string: How to convert back to list

I have an n-by-m Pandas DataFrame df defined as follows. (I know this is not the best way to do it. It makes sense for what I'm trying to do in my actual code, but that would be TMI for this post so ...
Gyan Veda's user avatar
  • 6,489
146 votes
8 answers
356k views

Using Python String Formatting with Lists

I construct a string s in Python 2.6.5 which will have a varying number of %s tokens, which match the number of entries in list x. I need to write out a formatted string. The following doesn't work, ...
SabreWolfy's user avatar
  • 5,490
140 votes
7 answers
399k views

Convert string to List<string> in one line?

I have a string: var names = "Brian,Joe,Chris"; Is there a way to convert this to a List<string> delimited by , in one line?
Brian David Berman's user avatar
137 votes
1 answer
197k views

Python: How to check a string for substrings from a list? [duplicate]

How can I check a string for substrings contained in a list, like in Check if a string contains an element from a list (of strings), but in Python?
user1045620's user avatar
  • 1,373
129 votes
6 answers
253k views

How do I convert a list into a string with spaces in Python?

How can I convert a list into a space-separated string in Python? For example, I want to convert this list: my_list = ["how", "are", "you"] into the string "how are ...
user1653402's user avatar
  • 1,403
127 votes
8 answers
206k views

Confused about __str__ on list in Python [duplicate]

Coming from a Java background, I understand that __str__ is something like a Python version of toString (while I do realize that Python is the older language). So, I have defined a little class along ...
Christofer Ohlsson's user avatar
119 votes
5 answers
695k views

Get the first character of the first string in a list?

How would I get the first character from the first string in a list in Python? It seems that I could use mylist[0][1:] but that does not give me the first character. >>> mylist = [] >&...
Trcx's user avatar
  • 4,264
112 votes
7 answers
131k views

string.Join on a List<int> or other type

I want to turn an array or list of ints into a comma delimited string, like this: string myFunction(List<int> a) { return string.Join(",", a); } But string.Join only takes List<string&...
Code Commander's user avatar
107 votes
5 answers
134k views

Python: find closest string (from a list) to another string

Let's say I have a string "Hello" and a list words = ['hello', 'Hallo', 'hi', 'house', 'key', 'screen', 'hallo','question', 'Hallo', 'format'] How can I find the n words that are the closest to "...
Laura's user avatar
  • 4,269
104 votes
6 answers
190k views

Scala check if element is present in a list

I need to check if a string is present in a list, and call a function which accepts a boolean accordingly. Is it possible to achieve this with a one liner? The code below is the best I could get: ...
Dario Oddenino's user avatar
96 votes
7 answers
96k views

How slow is Python's string concatenation vs. str.join?

As a result of the comments in my answer on this thread, I wanted to know what the speed difference is between the += operator and ''.join() So what is the speed comparison between the two?
Wayne Werner's user avatar
  • 50.5k
95 votes
16 answers
263k views

Converting a String to a List of Words?

I'm trying to convert a string to a list of words using python. I want to take something like the following: string = 'This is a string, with words!' Then convert to something like this : list = ['...
rectangletangle's user avatar
95 votes
3 answers
63k views

Converting a string representation of a list into an actual list object [duplicate]

I have a string that looks identical to a list, let's say: fruits = "['apple', 'orange', 'banana']" What would be the way to convert that to a list object?
Markum's user avatar
  • 3,959

1
2 3 4 5
142