moving position of character within an item in list
up vote
1
down vote
favorite
If i had a list e.g.
['Hello', 'what', 'is', 'your', 'name']
what method lets you move the position of a character within the item itself and store it. So 'Hello'
could be changed to 'elloH'
by moving first character to the end and the same applied to rest of the items.
python list items
add a comment |
up vote
1
down vote
favorite
If i had a list e.g.
['Hello', 'what', 'is', 'your', 'name']
what method lets you move the position of a character within the item itself and store it. So 'Hello'
could be changed to 'elloH'
by moving first character to the end and the same applied to rest of the items.
python list items
string slicing in list comprehension maybe
– Jean-François Fabre
Nov 10 at 18:03
See stackoverflow.com/questions/48607319/rotating-strings-in-python
– mkrieger1
Nov 10 at 18:09
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
If i had a list e.g.
['Hello', 'what', 'is', 'your', 'name']
what method lets you move the position of a character within the item itself and store it. So 'Hello'
could be changed to 'elloH'
by moving first character to the end and the same applied to rest of the items.
python list items
If i had a list e.g.
['Hello', 'what', 'is', 'your', 'name']
what method lets you move the position of a character within the item itself and store it. So 'Hello'
could be changed to 'elloH'
by moving first character to the end and the same applied to rest of the items.
python list items
python list items
edited Nov 10 at 18:08
petezurich
3,44881633
3,44881633
asked Nov 10 at 18:02
Programmer
157
157
string slicing in list comprehension maybe
– Jean-François Fabre
Nov 10 at 18:03
See stackoverflow.com/questions/48607319/rotating-strings-in-python
– mkrieger1
Nov 10 at 18:09
add a comment |
string slicing in list comprehension maybe
– Jean-François Fabre
Nov 10 at 18:03
See stackoverflow.com/questions/48607319/rotating-strings-in-python
– mkrieger1
Nov 10 at 18:09
string slicing in list comprehension maybe
– Jean-François Fabre
Nov 10 at 18:03
string slicing in list comprehension maybe
– Jean-François Fabre
Nov 10 at 18:03
See stackoverflow.com/questions/48607319/rotating-strings-in-python
– mkrieger1
Nov 10 at 18:09
See stackoverflow.com/questions/48607319/rotating-strings-in-python
– mkrieger1
Nov 10 at 18:09
add a comment |
3 Answers
3
active
oldest
votes
up vote
2
down vote
just rebuild the list and generate new strings with slicing & addition:
lst = ['Hello', 'what', 'is', 'your', 'name']
result = [x[1:]+x[0] if x else "" for x in lst]
result:
['elloH', 'hatw', 'si', 'oury', 'amen']
(note the ternary expression which allows robustness to empty strings, since int the case of empty strings x[0]
would raise an IndexError
. Without a ternary expression, we could use [x[1:]+x[0:1] for x in lst]
which does the same thing)
add a comment |
up vote
2
down vote
Check the below code :
mylist = ['Hello', 'what', 'is', 'your', 'name']
mylist = [(mylist[i][1:] + mylist[i][0:1]) for i in range(0,len(mylist))]
print(mylist)
Output :
['elloH', 'hatw', 'si', 'oury', 'amen']
1
Why not iterate over the list directly withoutrange
?
– bereal
Nov 10 at 18:10
1
the[0:1]
part is (maybe unwillingly) protecting against empty strings, but the part withrange
and indices is very unpythonic
– Jean-François Fabre
Nov 10 at 18:10
@bereal Iterating over the list will also do. Thanks for the suggestion.
– Sanchit Kumar
Nov 10 at 18:12
add a comment |
up vote
2
down vote
You could use slicing and indexing:
def shift(s):
return s[1:] + s[0]
data = ['Hello', 'what', 'is', 'your', 'name']
result = [shift(s) for s in data]
print(result)
Output
['elloH', 'hatw', 'si', 'oury', 'amen']
The statement result = [shift(s) for s in data]
is known as a list comprehension, is the equivalent of the following:
result =
for s in data:
result.append(shift(s))
Finally another alternative is to use map:
result = list(map(shift, data))
The function map applies shift to each element of data, but it returns (in Python 3) an iterable so you need to convert it to list.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241893%2fmoving-position-of-character-within-an-item-in-list%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
just rebuild the list and generate new strings with slicing & addition:
lst = ['Hello', 'what', 'is', 'your', 'name']
result = [x[1:]+x[0] if x else "" for x in lst]
result:
['elloH', 'hatw', 'si', 'oury', 'amen']
(note the ternary expression which allows robustness to empty strings, since int the case of empty strings x[0]
would raise an IndexError
. Without a ternary expression, we could use [x[1:]+x[0:1] for x in lst]
which does the same thing)
add a comment |
up vote
2
down vote
just rebuild the list and generate new strings with slicing & addition:
lst = ['Hello', 'what', 'is', 'your', 'name']
result = [x[1:]+x[0] if x else "" for x in lst]
result:
['elloH', 'hatw', 'si', 'oury', 'amen']
(note the ternary expression which allows robustness to empty strings, since int the case of empty strings x[0]
would raise an IndexError
. Without a ternary expression, we could use [x[1:]+x[0:1] for x in lst]
which does the same thing)
add a comment |
up vote
2
down vote
up vote
2
down vote
just rebuild the list and generate new strings with slicing & addition:
lst = ['Hello', 'what', 'is', 'your', 'name']
result = [x[1:]+x[0] if x else "" for x in lst]
result:
['elloH', 'hatw', 'si', 'oury', 'amen']
(note the ternary expression which allows robustness to empty strings, since int the case of empty strings x[0]
would raise an IndexError
. Without a ternary expression, we could use [x[1:]+x[0:1] for x in lst]
which does the same thing)
just rebuild the list and generate new strings with slicing & addition:
lst = ['Hello', 'what', 'is', 'your', 'name']
result = [x[1:]+x[0] if x else "" for x in lst]
result:
['elloH', 'hatw', 'si', 'oury', 'amen']
(note the ternary expression which allows robustness to empty strings, since int the case of empty strings x[0]
would raise an IndexError
. Without a ternary expression, we could use [x[1:]+x[0:1] for x in lst]
which does the same thing)
answered Nov 10 at 18:06
Jean-François Fabre
99.4k952109
99.4k952109
add a comment |
add a comment |
up vote
2
down vote
Check the below code :
mylist = ['Hello', 'what', 'is', 'your', 'name']
mylist = [(mylist[i][1:] + mylist[i][0:1]) for i in range(0,len(mylist))]
print(mylist)
Output :
['elloH', 'hatw', 'si', 'oury', 'amen']
1
Why not iterate over the list directly withoutrange
?
– bereal
Nov 10 at 18:10
1
the[0:1]
part is (maybe unwillingly) protecting against empty strings, but the part withrange
and indices is very unpythonic
– Jean-François Fabre
Nov 10 at 18:10
@bereal Iterating over the list will also do. Thanks for the suggestion.
– Sanchit Kumar
Nov 10 at 18:12
add a comment |
up vote
2
down vote
Check the below code :
mylist = ['Hello', 'what', 'is', 'your', 'name']
mylist = [(mylist[i][1:] + mylist[i][0:1]) for i in range(0,len(mylist))]
print(mylist)
Output :
['elloH', 'hatw', 'si', 'oury', 'amen']
1
Why not iterate over the list directly withoutrange
?
– bereal
Nov 10 at 18:10
1
the[0:1]
part is (maybe unwillingly) protecting against empty strings, but the part withrange
and indices is very unpythonic
– Jean-François Fabre
Nov 10 at 18:10
@bereal Iterating over the list will also do. Thanks for the suggestion.
– Sanchit Kumar
Nov 10 at 18:12
add a comment |
up vote
2
down vote
up vote
2
down vote
Check the below code :
mylist = ['Hello', 'what', 'is', 'your', 'name']
mylist = [(mylist[i][1:] + mylist[i][0:1]) for i in range(0,len(mylist))]
print(mylist)
Output :
['elloH', 'hatw', 'si', 'oury', 'amen']
Check the below code :
mylist = ['Hello', 'what', 'is', 'your', 'name']
mylist = [(mylist[i][1:] + mylist[i][0:1]) for i in range(0,len(mylist))]
print(mylist)
Output :
['elloH', 'hatw', 'si', 'oury', 'amen']
answered Nov 10 at 18:08
Sanchit Kumar
31117
31117
1
Why not iterate over the list directly withoutrange
?
– bereal
Nov 10 at 18:10
1
the[0:1]
part is (maybe unwillingly) protecting against empty strings, but the part withrange
and indices is very unpythonic
– Jean-François Fabre
Nov 10 at 18:10
@bereal Iterating over the list will also do. Thanks for the suggestion.
– Sanchit Kumar
Nov 10 at 18:12
add a comment |
1
Why not iterate over the list directly withoutrange
?
– bereal
Nov 10 at 18:10
1
the[0:1]
part is (maybe unwillingly) protecting against empty strings, but the part withrange
and indices is very unpythonic
– Jean-François Fabre
Nov 10 at 18:10
@bereal Iterating over the list will also do. Thanks for the suggestion.
– Sanchit Kumar
Nov 10 at 18:12
1
1
Why not iterate over the list directly without
range
?– bereal
Nov 10 at 18:10
Why not iterate over the list directly without
range
?– bereal
Nov 10 at 18:10
1
1
the
[0:1]
part is (maybe unwillingly) protecting against empty strings, but the part with range
and indices is very unpythonic– Jean-François Fabre
Nov 10 at 18:10
the
[0:1]
part is (maybe unwillingly) protecting against empty strings, but the part with range
and indices is very unpythonic– Jean-François Fabre
Nov 10 at 18:10
@bereal Iterating over the list will also do. Thanks for the suggestion.
– Sanchit Kumar
Nov 10 at 18:12
@bereal Iterating over the list will also do. Thanks for the suggestion.
– Sanchit Kumar
Nov 10 at 18:12
add a comment |
up vote
2
down vote
You could use slicing and indexing:
def shift(s):
return s[1:] + s[0]
data = ['Hello', 'what', 'is', 'your', 'name']
result = [shift(s) for s in data]
print(result)
Output
['elloH', 'hatw', 'si', 'oury', 'amen']
The statement result = [shift(s) for s in data]
is known as a list comprehension, is the equivalent of the following:
result =
for s in data:
result.append(shift(s))
Finally another alternative is to use map:
result = list(map(shift, data))
The function map applies shift to each element of data, but it returns (in Python 3) an iterable so you need to convert it to list.
add a comment |
up vote
2
down vote
You could use slicing and indexing:
def shift(s):
return s[1:] + s[0]
data = ['Hello', 'what', 'is', 'your', 'name']
result = [shift(s) for s in data]
print(result)
Output
['elloH', 'hatw', 'si', 'oury', 'amen']
The statement result = [shift(s) for s in data]
is known as a list comprehension, is the equivalent of the following:
result =
for s in data:
result.append(shift(s))
Finally another alternative is to use map:
result = list(map(shift, data))
The function map applies shift to each element of data, but it returns (in Python 3) an iterable so you need to convert it to list.
add a comment |
up vote
2
down vote
up vote
2
down vote
You could use slicing and indexing:
def shift(s):
return s[1:] + s[0]
data = ['Hello', 'what', 'is', 'your', 'name']
result = [shift(s) for s in data]
print(result)
Output
['elloH', 'hatw', 'si', 'oury', 'amen']
The statement result = [shift(s) for s in data]
is known as a list comprehension, is the equivalent of the following:
result =
for s in data:
result.append(shift(s))
Finally another alternative is to use map:
result = list(map(shift, data))
The function map applies shift to each element of data, but it returns (in Python 3) an iterable so you need to convert it to list.
You could use slicing and indexing:
def shift(s):
return s[1:] + s[0]
data = ['Hello', 'what', 'is', 'your', 'name']
result = [shift(s) for s in data]
print(result)
Output
['elloH', 'hatw', 'si', 'oury', 'amen']
The statement result = [shift(s) for s in data]
is known as a list comprehension, is the equivalent of the following:
result =
for s in data:
result.append(shift(s))
Finally another alternative is to use map:
result = list(map(shift, data))
The function map applies shift to each element of data, but it returns (in Python 3) an iterable so you need to convert it to list.
edited Nov 10 at 18:21
answered Nov 10 at 18:04
Daniel Mesejo
10.5k1923
10.5k1923
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241893%2fmoving-position-of-character-within-an-item-in-list%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
string slicing in list comprehension maybe
– Jean-François Fabre
Nov 10 at 18:03
See stackoverflow.com/questions/48607319/rotating-strings-in-python
– mkrieger1
Nov 10 at 18:09