Python Forum
Concatinating two strings to make a variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Concatinating two strings to make a variable
#5
(Jun-20-2017, 09:49 AM)bencouve Wrote: Perfect! Thanks ...

and ok

first post

Please don't do this. You're using the wrong data structure. Maybe you want to have a dict:

data = {'i0': 'This', 'i1': 'is', 'i2': 'the', 'i3': 'string'}

# direct access
print(data['i0'])

# iterating over values
# unordered

for value in data.values():
   print(value)

# sorted access
# sorted sorts by first element, if the the yielded element is a list/tuple
for key, value in sorted(data.items()):
   print(value)

# or preserve the order:
from collections import OrderedDict
data = [('i0', 'This'), ('i1', 'is'), ('i2', 'the'), ('i3', 'string')]
ordered_dict = OrderedDict(data)

for value in ordered_dict.values():
   print(value)
Since Python 3.6 dicts preserve the order, but we should not rely on it. Maybe with next version they guarantee it.
Currently it's just an implementation detail.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: Concatinating two strings to make a variable - by DeaD_EyE - Jun-20-2017, 06:08 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand strings and lists of strings Konstantin23 2 1,890 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Splitting strings in list of strings jesse68 3 2,700 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  extract particular strings into a variable lokesh 3 3,008 Dec-09-2020, 11:59 AM
Last Post: lokesh
  Finding multiple strings between the two same strings Slither 1 3,345 Jun-05-2019, 09:02 PM
Last Post: Yoriz
  How to I define a variable between strings in telnetlib write? Fez 2 4,334 May-02-2019, 06:53 PM
Last Post: Fez
  How do you make functions that take a variable that is not defined? magic 6 6,047 Sep-24-2018, 01:30 PM
Last Post: gruntfutuk
  Trying to make two different strings from the same root De1uge 0 2,583 Jun-09-2018, 04:06 PM
Last Post: De1uge
  Spyder IDE - make variable explorer to follow the color scheme of the Editor Antonio 0 6,382 May-05-2018, 10:20 PM
Last Post: Antonio
  lists, strings, and byte strings Skaperen 2 5,103 Mar-02-2018, 02:12 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020